forked from DengWangBao/Leetcode-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCombinations.java
More file actions
29 lines (23 loc) · 765 Bytes
/
Combinations.java
File metadata and controls
29 lines (23 loc) · 765 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
25
26
27
28
29
import java.util.ArrayList;
import java.util.List;
public class Combinations {
public List<List<Integer>> combine(int n, int k) {
List<List<Integer>> result = new ArrayList<List<Integer>>();
List<Integer> path = new ArrayList<Integer>();
combine(n, k, result, path, 1);
return result;
}
private void combine(int n, int k, List<List<Integer>> result, List<Integer> path, int start) {
if (k == 0) {
result.add(new ArrayList<Integer>(path));
return;
}
if (start > n) {
return;
}
path.add(start);
combine(n, k - 1, result, path, start + 1);
path.remove(path.size() - 1);
combine(n, k, result, path, start + 1);
}
}