class Solution {
List<List<Integer>> res = new ArrayList<>();
List<Integer> path = new ArrayList<>();
public List<List<Integer>> findSubsequences(int[] nums) {
backTracking(nums,0);
return res;
}
public void backTracking(int[] nums,int startIndex){
if(path.size() > 1){
res.add(new ArrayList<>(path));
}
if(nums.length == startIndex){
return;
}
Set<Integer> set = new HashSet<>();
for(int i=startIndex; i<nums.length; i++){
// 去重操作
if((!path.isEmpty() && nums[i] < path.get(path.size()-1)) || set.contains(nums[i])){
continue;
}
path.add(nums[i]);
set.add(nums[i]);
// 此处不需要对set进行回溯操作,因为每次递归都是new了一个set,一个set只会对本层的for循环中用的数进行去重
backTracking(nums,i+1);
path.remove(path.size()-1);
}
}
}
力扣491 递增子序列 Java版
最新推荐文章于 2024-06-07 08:25:31 发布