-
Notifications
You must be signed in to change notification settings - Fork 2
/
Solution.java
29 lines (25 loc) · 971 Bytes
/
Solution.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
public class Solution {
public List<List<Integer>> combine(int n, int k) {
LinkedList<List<Integer>> combinations =
new LinkedList<List<Integer>>();
LinkedList<Integer> currentCombination = new LinkedList<Integer>();
combineHelper(1, n, k, combinations, currentCombination);
return combinations;
}
private void combineHelper(int m, int n, int k,
LinkedList<List<Integer>> combinations,
LinkedList<Integer> currentCombination) {
if(k == 0) {
LinkedList<Integer> combination =
new LinkedList<Integer>(currentCombination);
combinations.add(combination);
return;
}
if(m > n)
return;
combineHelper(m + 1, n, k, combinations, currentCombination);
currentCombination.add(m);
combineHelper(m + 1, n, k - 1, combinations, currentCombination);
currentCombination.removeLast();
}
}