-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSubsetsDuplicate.java
More file actions
29 lines (25 loc) · 934 Bytes
/
SubsetsDuplicate.java
File metadata and controls
29 lines (25 loc) · 934 Bytes
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
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
class SubsetsDuplicate {
public static void main(String[] args) {
int[] nums = new int[]{1,2,2};
System.out.println(subsetsWithDup(nums).toString());
}
public static List<List<Integer>> subsetsWithDup(int[] nums) {
Arrays.sort(nums);
List<List<Integer>> result = new ArrayList<>();
dfsHelper(result, new ArrayList<>(),0,nums);
return result;
}
public static void dfsHelper(List<List<Integer>> result, List<Integer> subset, int start, int[] nums) {
result.add(new ArrayList<>(subset));
if(start == nums.length) return;
for(int i = start; i < nums.length; i++) {
if(i > start && nums[i] == nums[i-1]) continue;
subset.add(nums[i]);
dfsHelper(result,subset,i+1,nums);
subset.remove(subset.size()-1);
}
}
}