-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubsetsII.java
More file actions
99 lines (89 loc) · 2.61 KB
/
Copy pathSubsetsII.java
File metadata and controls
99 lines (89 loc) · 2.61 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package Lists;
import org.junit.Test;
import java.util.*;
/**
* @description: 描述 Medium
* @author: dekai.kong
* @date: 2018-12-30 18:35
* @from https://leetcode.com/problems/subsets-ii/
*
* Given a collection of integers that might contain duplicates, nums, return all possible subsets (the power set).
*
* Note: The solution set must not contain duplicate subsets.
*
* Example:
*
* Input: [1,2,2]
* Output:
* [
* [2],
* [1],
* [1,2,2],
* [2,2],
* [1,2],
* []
* ]
*/
public class SubsetsII {
public SubsetsII() {
}
/**
* Runtime: 10 ms, faster than 12.57% of Java online submissions for Subsets II.
* 递归解决
* @param nums
* @return
*/
public List<List<Integer>> subsetsWithDup2(int[] nums) {
List<List<Integer>> ans = new ArrayList<>();
ans.add(new ArrayList<>());
Arrays.sort(nums);
Set<String> set = new HashSet<>();
for (int i = 1; i <= nums.length; i++) {
helper(ans,i,0,new ArrayList<>(),nums,set);
}
return ans;
}
public void helper(List<List<Integer>> ans,int len,int start,List<Integer> curlist,int[] nums,Set<String> set){
if(len == 0){
if(set.add(curlist.toString()))
ans.add(new ArrayList<>(curlist));
}else{
for (int i = start; i < nums.length; i++) {
curlist.add(nums[i]);
helper(ans,len-1,i+1,curlist,nums,set);
curlist.remove(curlist.size()-1);
}
}
}
/**
* Runtime: 2 ms, faster than 98.79% of Java online submissions for Subsets II.
* @param nums
* @return
*/
public List<List<Integer>> subsetsWithDup(int[] nums) {
List<List<Integer>> ans = new ArrayList<>();
ans.add(new ArrayList<>());
Arrays.sort(nums);
for (int i = 1; i <= nums.length; i++) {
helper(ans,i,0,new ArrayList<>(),nums);
}
return ans;
}
public void helper(List<List<Integer>> ans,int len,int start,List<Integer> curlist,int[] nums){
if(len == 0){
ans.add(new ArrayList<>(curlist));
}else{
for (int i = start; i < nums.length; i++) {
if(i>start&&nums[i]==nums[i-1]) continue;//2ms
curlist.add(nums[i]);
helper(ans,len-1,i+1,curlist,nums);
curlist.remove(curlist.size()-1);
// while (i + 1 < nums.length && nums[i] == nums[i + 1]) ++i; 4ms
}
}
}
@Test
public void test() {
System.out.println(subsetsWithDup(new int[]{1,2,2}));
}
}