-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
30 lines (28 loc) · 954 Bytes
/
Solution.java
File metadata and controls
30 lines (28 loc) · 954 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
30
package leetcode._90_;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
class Solution {
public List<List<Integer>> subsetsWithDup(int[] nums) {
List<List<Integer>> result = new ArrayList<>();
List<Integer> elem = new ArrayList<>();
result.add(elem);
Arrays.sort(nums);
for (int i = 0; i < nums.length; i++) {
int dupCount = 0;
while (((i + 1) < nums.length) && nums[i + 1] == nums[i]) {
dupCount++;
i++;
}
int size = result.size();
for (int j = 0; j < size; j++) {
elem = new ArrayList<>(result.get(j));
for (int k = 0; k <= dupCount; k++) {
elem.add(nums[i]);
result.add(new ArrayList<>(elem)); // 注意这里是要放在 for 循环里面的
}
}
}
return result;
}
}