-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSubsetsII.java
More file actions
28 lines (24 loc) · 812 Bytes
/
SubsetsII.java
File metadata and controls
28 lines (24 loc) · 812 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
package com.dbc;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class SubsetsII {
private final List<List<Integer>> res = new ArrayList<>();
private void dfs(int start, int[] nums, List<Integer> remain, boolean select) {
if (start == nums.length) {
this.res.add(new ArrayList<>(remain));
return;
}
dfs(start + 1, nums, remain, false);
if (!select && start > 0 && nums[start] == nums[start - 1])
return;
remain.add(nums[start]);
dfs(start + 1, nums, remain, true);
remain.remove(remain.size() - 1);
}
public List<List<Integer>> subsetsWithDup(int[] nums) {
Arrays.sort(nums);
dfs(0, nums, new ArrayList<>(), false);
return this.res;
}
}