-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
24 lines (21 loc) · 691 Bytes
/
Copy pathSolution.java
File metadata and controls
24 lines (21 loc) · 691 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package leetcode._77_;
import java.util.ArrayList;
import java.util.List;
class Solution {
public List<List<Integer>> combine(int n, int k) {
List<List<Integer>> result = new ArrayList<List<Integer>>();
combine(result, new ArrayList<Integer>(), 1, n, k);
return result;
}
public void combine(List<List<Integer>> result, List<Integer> item, int start, int n, int k) {
if (k == 0) {
result.add(new ArrayList<Integer>(item));
return;
}
for (int i = start; i <= n; i++) {
item.add(i);
combine(result, item, i + 1, n, k - 1);
item.remove(item.size() - 1);
}
}
}