Skip to content

Commit f2c3511

Browse files
MEDIUM/src/medium/SubsetsII.java
1 parent 7e99a87 commit f2c3511

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

‎MEDIUM/src/medium/SubsetsII.java

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package medium;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.HashSet;
6+
import java.util.List;
7+
import java.util.Set;
8+
9+
import utils.CommonUtils;
10+
11+
public class SubsetsII {
12+
13+
public static List<List<Integer>> subsetsWithDup(int[] nums) {
14+
List<List<Integer>> result = new ArrayList();
15+
List<Integer> empty = new ArrayList();
16+
result.add(empty);
17+
if(nums == null) return result;
18+
Arrays.sort(nums);
19+
for(int i = 0; i < nums.length; i++){
20+
Set<List<Integer>> temp = new HashSet();
21+
for(List<Integer> list : result){
22+
List<Integer> newList = new ArrayList(list);
23+
newList.add(nums[i]);
24+
temp.add(newList);
25+
}
26+
result.addAll(temp);
27+
}
28+
Set<List<Integer>> resultSet = new HashSet();
29+
resultSet.addAll(result);
30+
result.clear();
31+
result.addAll(resultSet);
32+
return result;
33+
}
34+
35+
public static void main(String...args){
36+
int[] nums = new int[]{1,2,2};
37+
List<List<Integer>> result = subsetsWithDup(nums);
38+
CommonUtils.printIntegerList(result);
39+
}
40+
41+
}

0 commit comments

Comments
 (0)