forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSubsetsII.java
41 lines (35 loc) · 1.14 KB
/
SubsetsII.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
package medium;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import utils.CommonUtils;
public class SubsetsII {
public static List<List<Integer>> subsetsWithDup(int[] nums) {
List<List<Integer>> result = new ArrayList();
List<Integer> empty = new ArrayList();
result.add(empty);
if(nums == null) return result;
Arrays.sort(nums);
for(int i = 0; i < nums.length; i++){
Set<List<Integer>> temp = new HashSet();
for(List<Integer> list : result){
List<Integer> newList = new ArrayList(list);
newList.add(nums[i]);
temp.add(newList);
}
result.addAll(temp);
}
Set<List<Integer>> resultSet = new HashSet();
resultSet.addAll(result);
result.clear();
result.addAll(resultSet);
return result;
}
public static void main(String...args){
int[] nums = new int[]{1,2,2};
List<List<Integer>> result = subsetsWithDup(nums);
CommonUtils.printIntegerList(result);
}
}