-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy path1.java
31 lines (28 loc) · 980 Bytes
/
1.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
class Solution {
public List<List<Integer>> subsetsWithDup(int[] nums) {
// 对元素排序,保证相等的元素相邻
Arrays.sort(nums);
Deque<Integer> current = new ArrayDeque<>(nums.length);
List<List<Integer>> res = new ArrayList<>();
backtrack(nums, 0, current, res);
return res;
}
// 候选集合: nums[k..N)
void backtrack(int[] nums, int k, Deque<Integer> current, List<List<Integer>> res) {
if (k == nums.length) {
res.add(new ArrayList<>(current));
return;
}
// 选择 nums[k]
current.addLast(nums[k]);
backtrack(nums, k+1, current, res);
current.removeLast();
// 不选择 nums[k]
// 将后续和 nums[k] 相等的元素 nums[k..j) 都从候选集合中删除
int j = k;
while (j < nums.length && nums[j] == nums[k]) {
j++;
}
backtrack(nums, j, current, res);
}
}