文章目录
力扣算法学习day20-3
78-子集
题目
代码实现
class Solution {
List<List<Integer>> result = new ArrayList<>();
List<Integer> path = new ArrayList<>();
public List<List<Integer>> subsets(int[] nums) {
recall(0,nums);
return result;
}
public void recall(int i,int[] nums){
result.add(new ArrayList<>(path));
for(int j = i;j < nums.length;j++){
path.add(nums[j]);
recall(j+1,nums);
path.remove(path.size()-1);
}
}
}
90-子集 II
题目
代码实现
class Solution {
List<List<Integer>> result = new ArrayList<>();
List<Integer> path = new ArrayList<>();
public List<List<Integer>> subsetsWithDup(int[] nums) {
Arrays.sort(nums);
recall(0,nums);
return result;
}
public void recall(int i,int[] nums){
result.add(new ArrayList<>(path));
for(int j = i;j < nums.length;j++){
if(j > i && nums[j] == nums[j - 1]){
continue;
}
path.add(nums[j]);
recall(j+1,nums);
path.remove(path.size()-1);
}
}
}