-
-
Notifications
You must be signed in to change notification settings - Fork 291
Expand file tree
/
Copy path90.java
More file actions
42 lines (40 loc) · 1.64 KB
/
90.java
File metadata and controls
42 lines (40 loc) · 1.64 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
33
34
35
36
37
38
39
40
41
42
__________________________________________________________________________________________________
sample 1 ms submission
class Solution {
public List<List<Integer>> subsetsWithDup(int[] nums) {
List<List<Integer>> result = new ArrayList<List<Integer>>();
List<Integer> sr = new ArrayList<Integer>();
Arrays.sort(nums);
helper(result,sr,nums,0);
return result;
}
public void helper(List<List<Integer>> result, List<Integer> sr, int[] nums, int level){
result.add(sr);
for(int i=level;i<nums.length;++i){
sr.add(nums[i]);
helper(result,new ArrayList(sr),nums,i+1);
sr.remove(sr.size()-1);
while(i+1<nums.length&&nums[i]==nums[i+1])i++;
}
}
}
__________________________________________________________________________________________________
sample 34828 kb submission
class Solution {
public List<List<Integer>> subsetsWithDup(int[] nums) {
List<List<Integer>> list = new ArrayList<>();
Arrays.sort(nums);
backtrack(list, new ArrayList<>(), nums, 0);
return list;
}
private void backtrack(List<List<Integer>> list, List<Integer> tempList, int[] nums, int start) {
list.add(new ArrayList<>(tempList));
for (int i = start; i < nums.length; i++) {
if (i > start && nums[i] == nums[i - 1]) continue;//skip duplicate
tempList.add(nums[i]);
backtrack(list, tempList, nums, i + 1);
tempList.remove(tempList.size() - 1);
}
}
}
__________________________________________________________________________________________________