-
Notifications
You must be signed in to change notification settings - Fork 0
/
SubsetsII.java
28 lines (26 loc) · 1.02 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
import java.util.ArrayList;
import java.util.Arrays;
public class Solution {
public ArrayList<ArrayList<Integer>> subsetsWithDup(int[] num) {
Arrays.sort(num);
int[] picked = new int[num.length];
ArrayList<ArrayList<Integer>> results = new ArrayList<ArrayList<Integer>>();
subsetsWithDupHelper(num, 0, picked, 0, results);
return results;
}
private void subsetsWithDupHelper(int[] num, int index, int[] picked, int pidx, ArrayList<ArrayList<Integer>> results) {
if (index >= num.length) {
ArrayList<Integer> set = new ArrayList<Integer>();
for (int i = 0; i < pidx; i++)
set.add(picked[i]);
results.add(set);
return;
}
int next = index + 1;
while (next < num.length && num[next] == num[index])
next++;
subsetsWithDupHelper(num, next, picked, pidx, results);
picked[pidx] = num[index];
subsetsWithDupHelper(num, index + 1, picked, pidx + 1, results);
}
}