给定一个整型数组, 你的任务是找到所有该数组的递增子序列,递增子序列的长度至少是2。
示例:
输入: [4, 6, 7, 7]
输出: [[4, 6], [4, 7], [4, 6, 7], [4, 6, 7, 7], [6, 7], [6, 7, 7], [7,7], [4,7,7]]
代码
class Solution {
List<List<Integer>> res=new ArrayList<>();
Set<List<Integer>> s=new HashSet<>();//去重
public List<List<Integer>> findSubsequences(int[] nums) {
getFindSubsequences(nums,0,new ArrayList<>());
return res;
}
public void getFindSubsequences(int[] nums,int loc,List<Integer> tmp) {/回溯
if(tmp.size()>=2&&!s.contains(tmp)) {
res.add(new ArrayList<>(tmp));
s.add(new ArrayList<>(tmp));
}
if(loc==nums.length) return;
for(int i=loc;i<nums.length;i++)
{
if(tmp.size()!=0&&nums[i]<tmp.get(tmp.size()-1))//判断递增
continue;
tmp.add(nums[i]);
getFindSubsequences(nums, i+1, tmp);
tmp.remove(tmp.size()-1);
}
}
}