Skip to content

Commit b2c80a4

Browse files
edit 90
1 parent 63f6679 commit b2c80a4

File tree

1 file changed

+38
-27
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+38
-27
lines changed
Lines changed: 38 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
11
package com.fishercoder.solutions;
22

3-
import com.fishercoder.common.utils.CommonUtils;
4-
53
import java.util.*;
64

75
/**90. Subsets II
86
*
9-
* Given a collection of integers that might contain duplicates, nums, return all possible subsets.
10-
7+
*Given a collection of integers that might contain duplicates, nums, return all possible subsets.
118
Note: The solution set must not contain duplicate subsets.
129
1310
For example,
1411
If nums = [1,2,2], a solution is:
15-
1612
[
1713
[2],
1814
[1],
@@ -24,32 +20,47 @@
2420
*/
2521
public class _90 {
2622

27-
public static List<List<Integer>> subsetsWithDup(int[] nums) {
28-
List<List<Integer>> result = new ArrayList();
29-
List<Integer> empty = new ArrayList();
30-
result.add(empty);
31-
if(nums == null) return result;
32-
Arrays.sort(nums);
33-
for(int i = 0; i < nums.length; i++){
34-
Set<List<Integer>> temp = new HashSet();
35-
for(List<Integer> list : result){
36-
List<Integer> newList = new ArrayList(list);
37-
newList.add(nums[i]);
38-
temp.add(newList);
23+
public static class IterativeSolution {
24+
public static List<List<Integer>> subsetsWithDup(int[] nums) {
25+
List<List<Integer>> result = new ArrayList();
26+
List<Integer> empty = new ArrayList();
27+
result.add(empty);
28+
if (nums == null) return result;
29+
Arrays.sort(nums);
30+
for (int i = 0; i < nums.length; i++) {
31+
Set<List<Integer>> temp = new HashSet();
32+
for (List<Integer> list : result) {
33+
List<Integer> newList = new ArrayList(list);
34+
newList.add(nums[i]);
35+
temp.add(newList);
36+
}
37+
result.addAll(temp);
3938
}
40-
result.addAll(temp);
39+
Set<List<Integer>> resultSet = new HashSet();
40+
resultSet.addAll(result);
41+
result.clear();
42+
result.addAll(resultSet);
43+
return result;
4144
}
42-
Set<List<Integer>> resultSet = new HashSet();
43-
resultSet.addAll(result);
44-
result.clear();
45-
result.addAll(resultSet);
46-
return result;
4745
}
4846

49-
public static void main(String...args){
50-
int[] nums = new int[]{1,2,2};
51-
List<List<Integer>> result = subsetsWithDup(nums);
52-
CommonUtils.printListList(result);
47+
public static class BacktrackingSolution {
48+
public List<List<Integer>> subsetsWithDup(int[] nums) {
49+
List<List<Integer>> result = new ArrayList();
50+
Arrays.sort(nums);
51+
backtrack(nums, 0, new ArrayList(), result);
52+
return result;
53+
}
54+
55+
void backtrack(int[] nums, int start, List<Integer> curr, List<List<Integer>> result) {
56+
result.add(new ArrayList(curr));
57+
for (int i = start; i < nums.length; i++) {
58+
if (i > start && nums[i] == nums[i - 1]) continue;
59+
curr.add(nums[i]);
60+
backtrack(nums, i + 1, curr, result);
61+
curr.remove(curr.size() - 1);
62+
}
63+
}
5364
}
5465

5566
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy