491. Increasing Subsequences

网友投稿 691 2022-10-09

491. Increasing Subsequences

491. Increasing Subsequences

Given an integer array, your task is to find all the different possible increasing subsequences of the given array, and the length of an increasing subsequence should be at least 2 .

Example:

Input: [4, 6, 7, 7]Output: [[4, 6], [4, 7], [4, 6, 7], [4, 6, 7, 7], [6, 7], [6, 7, 7], [7,7], [4,7,7]]

Note: The length of the given array will not exceed 15. The range of integer in the given array is [-100,100]. The given array may contain duplicates, and two equal integers should also be considered as a special case of increasing sequence.

class Solution public List> findSubsequences(int[] nums) { Set> list = new HashSet>(); backtrack(list, new ArrayList<>(), nums, 0); return new ArrayList(list); } private void backtrack(Set> list, List tempList, int[] nums, int start){ if (tempList.size() > 1) list.add(new ArrayList<>(tempList)); for(int i = start; i < nums.length; i++){ if(tempList.size() == 0 || tempList.get(tempList.size() - 1) <= nums[i]){ tempList.add(nums[i]); backtrack(list, tempList, nums, i + 1); tempList.remove(tempList.size() - 1); } } } }

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:[wechat keyboard] 微信小程序无序键盘(wechat已停止工作怎么办)
下一篇:498. Diagonal Traverse
相关文章

 发表评论

暂时没有评论,来抢沙发吧~