Skip to content

Commit 63f6679

Browse files
edit 78
1 parent 82c1ea2 commit 63f6679

File tree

2 files changed

+39
-43
lines changed

2 files changed

+39
-43
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,7 @@ Your ideas/fixes/algorithms are more than welcome!
519519
|81|[Search in Rotated Sorted Array II](https://leetcode.com/problems/search-in-rotated-sorted-array-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_81.java)|O(logn)|O(1)|Medium|Binary Search
520520
|80|[Remove Duplicates from Sorted Array II](https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_80.java)|O(n) |O(n)|Medium|
521521
|79|[Word Search](https://leetcode.com/problems/word-search/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_79.java)|O(m*n*l) ? |O(m*n)|Medium|Backtracking, DFS
522-
|78|[Subsets](https://leetcode.com/problems/subsets/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_78.java)|O(n^2) ? |O(1)|Medium|Backtracking
522+
|78|[Subsets](https://leetcode.com/problems/subsets/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_78.java)|O(n^2) |O(1)|Medium|Backtracking
523523
|77|[Combinations](https://leetcode.com/problems/combinations/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_77.java)|O(n^2) ? |O(1)|Medium|Backtracking
524524
|76|[Minimum Window Substring](https://leetcode.com/problems/minimum-window-substring/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_76.java)|O(n)|O(k)|Hard|Two Pointers
525525
|75|[Sort Colors](https://leetcode.com/problems/sort-colors/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_75.java)|O(n)|O(1)|Medium| Two Pointers
Lines changed: 38 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
package com.fishercoder.solutions;
22

3-
import com.fishercoder.common.utils.CommonUtils;
4-
53
import java.util.ArrayList;
64
import java.util.List;
75

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.
1110
1211
For example,
1312
If nums = [1,2,3], a solution is:
14-
1513
[
1614
[3],
1715
[1],
@@ -26,46 +24,44 @@
2624

2725
public class _78 {
2826

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);
4743
}
48-
result.addAll(temp);
44+
return result;
4945
}
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+
5947
}
6048

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+
}
6964
}
65+
7066
}
7167
}

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