【DFS】LeetCode 90. 子集 II
题目链接
思路
去重方法与【DFS】LeetCode 40. 组合总和 II思路相似
代码
class Solution {
private List<List<Integer>> result = new ArrayList<>();
private Deque<Integer> path = new ArrayDeque<>();
public List<List<Integer>> subsetsWithDup(int[] nums) {
Arrays.sort(nums);
dfs(0, nums);
return result;
}
void dfs(int index, int[] nums){
result.add(new ArrayList<>(path));
if(index >= nums.length){
return;
}
for(int i = index; i < nums.length; i++){
if(i != index && nums[i - 1] == nums[i]){
continue;
}
path.addLast(nums[i]);
dfs(i + 1, nums);
path.removeLast();
}
}
}