|
1 | 1 | package com.fishercoder.solutions;
|
2 | 2 |
|
3 |
| -import com.fishercoder.common.utils.CommonUtils; |
4 |
| - |
5 | 3 | import java.util.ArrayList;
|
6 | 4 | import java.util.List;
|
7 | 5 |
|
8 |
| -/** Given a set of distinct integers, nums, return all possible subsets. |
9 |
| -
|
10 |
| - Note: The solution set must not contain duplicate subsets. |
| 6 | +/** |
| 7 | + * 78. Subsets |
| 8 | + * Given a set of distinct integers, nums, return all possible subsets. |
| 9 | + * Note: The solution set must not contain duplicate subsets. |
11 | 10 |
|
12 | 11 | For example,
|
13 | 12 | If nums = [1,2,3], a solution is:
|
14 |
| -
|
15 | 13 | [
|
16 | 14 | [3],
|
17 | 15 | [1],
|
|
26 | 24 |
|
27 | 25 | public class _78 {
|
28 | 26 |
|
29 |
| - public static void main(String...strings){ |
30 |
| -// int[] nums = new int[]{1,2,3}; |
31 |
| - int[] nums = new int[]{1,2,2}; |
32 |
| - List<List<Integer>> result = subsets_backtracking(nums); |
33 |
| - CommonUtils.printListList(result); |
34 |
| - } |
35 |
| - |
36 |
| - public static List<List<Integer>> subsets(int[] nums) { |
37 |
| - List<List<Integer>> result = new ArrayList(); |
38 |
| - List<Integer> empty = new ArrayList(); |
39 |
| - result.add(empty); |
40 |
| - if(nums == null) return result; |
41 |
| - for(int i = 0; i < nums.length; i++){ |
42 |
| - List<List<Integer>> temp = new ArrayList();//you'll have to create a new one here, otherwise, it'll throw ConcurrentModificationException. |
43 |
| - for(List<Integer> list : result){ |
44 |
| - List<Integer> newList = new ArrayList(list); |
45 |
| - newList.add(nums[i]); |
46 |
| - temp.add(newList); |
| 27 | + public static class IterativeSolution { |
| 28 | + |
| 29 | + public static List<List<Integer>> subsets(int[] nums) { |
| 30 | + List<List<Integer>> result = new ArrayList(); |
| 31 | + List<Integer> empty = new ArrayList(); |
| 32 | + result.add(empty); |
| 33 | + if (nums == null) return result; |
| 34 | + for (int i = 0; i < nums.length; i++) { |
| 35 | + List<List<Integer>> temp = new ArrayList(); |
| 36 | + //you'll have to create a new one here, otherwise, it'll throw ConcurrentModificationException. |
| 37 | + for (List<Integer> list : result) { |
| 38 | + List<Integer> newList = new ArrayList(list); |
| 39 | + newList.add(nums[i]); |
| 40 | + temp.add(newList); |
| 41 | + } |
| 42 | + result.addAll(temp); |
47 | 43 | }
|
48 |
| - result.addAll(temp); |
| 44 | + return result; |
49 | 45 | }
|
50 |
| - return result; |
51 |
| - } |
52 |
| - |
53 |
| - /**this post: https://discuss.leetcode.com/topic/46159/a-general-approach-to-backtracking-questions-in-java-subsets-permutations-combination-sum-palindrome-partitioning |
54 |
| - * is really cool!*/ |
55 |
| - public static List<List<Integer>> subsets_backtracking(int[] nums) { |
56 |
| - List<List<Integer>> result = new ArrayList(); |
57 |
| - backtracking(result, new ArrayList(), nums, 0); |
58 |
| - return result; |
| 46 | + |
59 | 47 | }
|
60 | 48 |
|
61 |
| - private static void backtracking(List<List<Integer>> result, List<Integer> temp, int[] nums, int start) { |
62 |
| - //ATTN: you'll have to make a new list here before entering the for loop |
63 |
| - result.add(new ArrayList(temp)); |
64 |
| - for(int i = start; i < nums.length; i++){ |
65 |
| - if(i != start && nums[i] == nums[i-1]) continue;//add this line here to skip duplicates for _78 II |
66 |
| - temp.add(nums[i]); |
67 |
| - backtracking(result, temp, nums, i+1); |
68 |
| - temp.remove(temp.size()-1); |
| 49 | + public static class BacktrackingSolution { |
| 50 | + |
| 51 | + public List<List<Integer>> subsets(int[] nums) { |
| 52 | + List<List<Integer>> result = new ArrayList(); |
| 53 | + backtracking(result, new ArrayList(), nums, 0); |
| 54 | + return result; |
| 55 | + } |
| 56 | + |
| 57 | + void backtracking(List<List<Integer>> result, List<Integer> temp, int[] nums, int start) { |
| 58 | + result.add(new ArrayList(temp)); |
| 59 | + for (int i = start; i < nums.length; i++) { |
| 60 | + temp.add(nums[i]); |
| 61 | + backtracking(result, temp, nums, i + 1); |
| 62 | + temp.remove(temp.size() - 1); |
| 63 | + } |
69 | 64 | }
|
| 65 | + |
70 | 66 | }
|
71 | 67 | }
|
0 commit comments