-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCombinations.java
More file actions
67 lines (42 loc) · 1012 Bytes
/
Combinations.java
File metadata and controls
67 lines (42 loc) · 1012 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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package company;
import java.util.ArrayList;
import java.util.List;
/**
* @Author: Wenhang Chen
* @Description:给定两个整数 n 和 k,返回 1 ... n 中所有可能的 k 个数的组合。
* <p>
* 示例:
* <p>
* 输入: n = 4, k = 2
* 输出:
* [
* [2,4],
* [3,4],
* [2,3],
* [1,2],
* [1,3],
* [1,4],
* ]
* @Date: Created in 9:22 2/27/2020
* @Modified by:
*/
public class Combinations {
List<List<Integer>> res = new ArrayList<>();
public List<List<Integer>> combine(int n, int k) {
helper(1, new ArrayList<>(), n, k);
return res;
}
public void helper(int num, List<Integer> solution, int n, int k) {
if (solution.size() == k) {
res.add(new ArrayList<>(solution));
}
if (num > n) {
return;
}
for (int i = num; i <= n; i++) {
solution.add(i);
helper(i + 1, solution, n, k);
solution.remove(solution.size() - 1);
}
}
}