90. 子集 II

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution {
List<List<Integer>> ans = new ArrayList<>();
Set<List<Integer>> set = new HashSet<>();
public List<List<Integer>> subsetsWithDup(int[] nums) {
Arrays.sort(nums);
dfs(0, nums, new ArrayList<>());
return ans;
}

public void dfs(int cur, int[] nums, List<Integer> list){
if (!set.contains(list)){
List<Integer> copy = new ArrayList<>(list);
set.add(copy);
ans.add(copy);
}
for (int i = cur; i < nums.length; i++) {
list.add(nums[i]);
dfs(i+1, nums, list);
list.remove(list.size()-1);
}
}
}