-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsubsetsII.java
More file actions
32 lines (31 loc) · 1.01 KB
/
subsetsII.java
File metadata and controls
32 lines (31 loc) · 1.01 KB
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
import java.util.*;
public class subsetsII {
public static void main(String[] args){
subsetsII obj = new subsetsII();
int[] nums = {1,2,2,4};
System.out.println(obj.subsetsWithDup(nums));
}
private List<List<Integer>> subsetsWithDup(int[] nums){
List<List<Integer>> res = new ArrayList<>();
List<Integer> path = new ArrayList<>();
dfs(nums, 0, path, res);
return res;
}
private void dfs(int[] nums, int pos, List<Integer> path, List<List<Integer>> res){
if(pos > nums.length){
return;
}
res.add(new ArrayList<>(path));
for(int i = pos; i < nums.length; i++){
/* remove duplicates
In the recursion tree, for each node, we need to remove duplicate branches.
* */
if(i > pos && nums[i] == nums[i-1]){
continue;
}
path.add(nums[i]);
dfs(nums, i+1, path, res);
path.remove(path.size()-1);
}
}
}