Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit cc0aa8e

Browse files
solves subsets
1 parent 0719b16 commit cc0aa8e

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
| 74 | [Search a 2D Matrix](https://leetcode.com/problems/search-a-2d-matrix) | [![Java](assets/java.png)](src/SearchA2DMatrix.java) | |
7272
| 75 | [Sort Colors](https://leetcode.com/problems/sort-colors) | [![Java](assets/java.png)](src/SortColors.java) | |
7373
| 77 | [Combinations](https://leetcode.com/problems/combinations) | [![Java](assets/java.png)](src/Combinations.java) | |
74+
| 78 | [Subsets](https://leetcode.com/problems/subsets) | [![Java](assets/java.png)](src/Subsets.java) | |
7475
| 83 | [Remove Duplicates from Sorted List](https://leetcode.com/problems/remove-duplicates-from-sorted-list) | [![Java](assets/java.png)](src/RemoveDuplicatesFromSortedList.java) [![Python](assets/python.png)](python/remove_duplicates_from_linked_list.py) | |
7576
| 88 | [Merge Sorted Array](https://leetcode.com/problems/merge-sorted-array) | [![Java](assets/java.png)](src/MergeSortedArray.java) [![Python](assets/python.png)](python/merge_sorted_array.py) | |
7677
| 94 | [Binary Tree Inorder Traversal](https://leetcode.com/problems/binary-tree-inorder-traversal/) | [![Java](assets/java.png)](src/BinaryTreeInorderTraversal.java) [![Python](assets/python.png)](python/binary_tree_inorder_traversal.py) | |

src/Subsets.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// https://leetcode.com/problems/subsets
2+
// T: O(n * 2^n)
3+
// S: O(n)
4+
5+
import java.util.ArrayList;
6+
import java.util.LinkedList;
7+
import java.util.List;
8+
9+
public class Subsets {
10+
public List<List<Integer>> subsets(int[] nums) {
11+
final List<List<Integer>> result = new ArrayList<>();
12+
addSubsets(nums, 0, result, new LinkedList<>());
13+
return result;
14+
}
15+
16+
private void addSubsets(int[] array, int index, List<List<Integer>> result, LinkedList<Integer> current) {
17+
if (index == array.length) {
18+
result.add(new ArrayList<>(current));
19+
return;
20+
}
21+
22+
current.add(array[index]);
23+
addSubsets(array, index + 1, result, current);
24+
current.removeLast();
25+
addSubsets(array, index + 1, result, current);
26+
}
27+
}

0 commit comments

Comments
 (0)