-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCombinations_77.java
36 lines (33 loc) · 991 Bytes
/
Combinations_77.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
30
31
32
33
34
35
36
/**
* DFS
* - LinkedList vs Stack
* LinkedList is pushed and poped in the front
* Stack is pushed and poped in the end
*
* - Another Way:
* C(n,k) = C(n-1, k-1) U n + C(n-1,k)
* However, this will require a lot of copy.
* So I do not think this is a good method, but good try
*/
public class Solution {
List<List<Integer>> res = new ArrayList<>();
public List<List<Integer>> combine(int n, int k) {
if (k > n || n == 0 || k == 0) return res;
Stack<Integer> stack = new Stack<>();
// XXXX the initial state is empty stack
combine(n, k, stack, 0, 1);
return res;
}
private void combine(int n, int k, Stack<Integer> stack,
int level, int start) {
if (level == k) {
res.add((List)(stack.clone()));
return;
}
for(int i=start; i<=n; i++) {
stack.push(i);
combine(n, k, stack, level+1, i+1);
stack.pop();
}
}
}