Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0719b16

Browse files
solves combinations
1 parent e5d6c58 commit 0719b16

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
| 73 | [Set Matrix Zeroes](https://leetcode.com/problems/set-matrix-zeroes) | [![Java](assets/java.png)](src/SetMatrixZeroes.java) | |
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) | |
73+
| 77 | [Combinations](https://leetcode.com/problems/combinations) | [![Java](assets/java.png)](src/Combinations.java) | |
7374
| 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) | |
7475
| 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) | |
7576
| 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/Combinations.java

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

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /