-
Notifications
You must be signed in to change notification settings - Fork 0
/
090_SubsetsII90.java
92 lines (78 loc) · 2.58 KB
/
090_SubsetsII90.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
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
/**
* 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.
*
* For example,
* If nums = [1,2,2], a solution is:
*
* [
* [2],
* [1],
* [1,2,2],
* [2,2],
* [1,2],
* []
* ]
*/
public class SubsetsII90 {
public List<List<Integer>> subsetsWithDup(int[] nums) {
Set<List<Integer>> res = new HashSet<>();
Map<Integer, Integer> co = new HashMap<>();
helper(0, nums, co, res);
return new ArrayList<List<Integer>>(res);
}
public void helper(int pos, int[] nums, Map<Integer, Integer> co, Set<List<Integer>> res) {
if (pos >= nums.length) {
res.add(mapToList(co));
return;
}
res.add(mapToList(co));
helper(pos+1, nums, co, res);
co.put(nums[pos], co.getOrDefault(nums[pos], 0) + 1);
helper(pos+1, nums, co, res);
co.put(nums[pos], co.get(nums[pos]) - 1);
}
public List<Integer> mapToList(Map<Integer, Integer> co) {
List<Integer> l = new ArrayList<Integer>();
for (Map.Entry<Integer, Integer> e: co.entrySet()) {
for (int i=0; i<e.getValue(); i++) {
l.add(e.getKey());
}
}
return l;
}
public List<List<Integer>> subsetsWithDup2(int[] nums) {
Arrays.sort(nums);
Set<List<Integer>> res = new HashSet<>();
List<Integer> each = new ArrayList<>();
helper2(0, nums, each, res);
return new ArrayList<List<Integer>>(res);
}
public void helper2(int pos, int[] nums, List<Integer> each, Set<List<Integer>> res) {
if (pos >= nums.length) {
res.add(new ArrayList<Integer>(each));
return;
}
res.add(new ArrayList<Integer>(each));
helper(pos+1, nums, each, res);
each.add(nums[pos]);
helper(pos+1, nums, each, res);
each.remove(each.size()-1);
}
public List<List<Integer>> subsetsWithDup3(int[] nums) {
Arrays.sort(nums);
List<List<Integer>> res = new ArrayList<>();
helper3(0, nums, new ArrayList<>(), res);
return res;
}
public void helper3(int pos, int[] nums, List<Integer> each, List<List<Integer>> res) {
if (pos <= nums.length) res.add(new ArrayList<Integer>(each));
for(int i=pos; i < nums.length; i++){
if(i > pos && nums[i] == nums[i-1]) continue;
each.add(nums[i]);
helper(i+1, nums, each, res);
each.remove(each.size()-1);
}
}
}