-
Notifications
You must be signed in to change notification settings - Fork 37
/
90 Subsets II
35 lines (28 loc) · 931 Bytes
/
90 Subsets II
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
Given a collection of integers that might contain duplicates, nums, return all possible subsets (the power set).
Note: The solution set must not contain duplicate subsets.
Example:
Input: [1,2,2]
Output:
[
[2],
[1],
[1,2,2],
[2,2],
[1,2],
[]
]
public List<List<Integer>> subsetsWithDup(int[] nums) {
Arrays.sort(nums);
List<List<Integer>> subset = new ArrayList();
dfs(subset,0,nums,new ArrayList());
return subset;
}
void dfs( List<List<Integer>> subset,int index,int[] nums,List<Integer> current){
subset.add(new ArrayList(current));
for(int i=index;i<nums.length;i++){
if(i>index && nums[i]==nums[i-1])continue;
current.add(nums[i]);
dfs(subset,i+1,nums, current);
current.remove(current.size()-1);
}
}