forked from tanglu/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubsetsII.java
34 lines (32 loc) · 1.08 KB
/
subsetsII.java
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
public class Solution {
void dfs(int[] number_array, int start, int number, ArrayList<Integer> array, ArrayList<ArrayList<Integer>> result) {
if(array.size()==number) {
result.add(new ArrayList<Integer>(array));
return;
}
int i = start;
while(i<number_array.length) {
array.add(number_array[i]);
dfs(number_array,i+1,number,array,result);
array.remove(array.size()-1);
while(i<(number_array.length-1)&&number_array[i]==number_array[i+1]) {
i++;
}
i++;
}
}
public ArrayList<ArrayList<Integer>> subsetsWithDup(int[] num) {
ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
ArrayList<Integer> array = new ArrayList<Integer>();
result.add(array);
if(num==null) {
return result;
}
Arrays.sort(num);
for(int i=1;i<=num.length;i++) {
array.clear();
dfs(num,0,i,array,result);
}
return result;
}
}