|
1 | 1 | package com.fishercoder.solutions;
|
2 | 2 |
|
3 |
| -import com.fishercoder.common.utils.CommonUtils; |
4 |
| - |
5 | 3 | import java.util.*;
|
6 | 4 |
|
7 | 5 | /**90. Subsets II
|
8 | 6 | *
|
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. |
11 | 8 | Note: The solution set must not contain duplicate subsets.
|
12 | 9 |
|
13 | 10 | For example,
|
14 | 11 | If nums = [1,2,2], a solution is:
|
15 |
| -
|
16 | 12 | [
|
17 | 13 | [2],
|
18 | 14 | [1],
|
|
24 | 20 | */
|
25 | 21 | public class _90 {
|
26 | 22 |
|
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); |
39 | 38 | }
|
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; |
41 | 44 | }
|
42 |
| - Set<List<Integer>> resultSet = new HashSet(); |
43 |
| - resultSet.addAll(result); |
44 |
| - result.clear(); |
45 |
| - result.addAll(resultSet); |
46 |
| - return result; |
47 | 45 | }
|
48 | 46 |
|
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 | + } |
53 | 64 | }
|
54 | 65 |
|
55 | 66 | }
|
0 commit comments