-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubsetsWithDup.java
More file actions
36 lines (30 loc) · 859 Bytes
/
SubsetsWithDup.java
File metadata and controls
36 lines (30 loc) · 859 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
31
32
33
34
35
36
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* 90. 子集II
*/
public class SubsetsWithDup {
public List<List<Integer>> subsetsWithDup(int[] nums) {
List<List<Integer>> res = new ArrayList<>();
// 先进行排序
Arrays.sort(nums);
backtrack(0, nums, new ArrayList<>(), res);
return res;
}
void backtrack(int start, int[] nums, List<Integer> temp, List<List<Integer>> res){
if(start>nums.length){
return;
}
res.add(new ArrayList<>(temp));
for(int i=start; i<nums.length; i++){
// 去重
if(i>start && nums[i]==nums[i-1]){
continue;
}
temp.add(nums[i]);
backtrack(i+1, nums, temp, res);
temp.remove(temp.size()-1);
}
}
}