forked from algorithm022/algorithm022
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCombinations.java
More file actions
60 lines (54 loc) · 1.37 KB
/
Copy pathCombinations.java
File metadata and controls
60 lines (54 loc) · 1.37 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package practice.recursion;
import java.util.ArrayList;
import java.util.List;
/**
* Given two integers n and k, return all possible combinations of k numbers out of 1 ... n.
* <p>
* You may return the answer in any order.
* <p>
*
* <p>
* Example 1:
* <p>
* Input: n = 4, k = 2
* Output:
* [
* [2,4],
* [3,4],
* [2,3],
* [1,2],
* [1,3],
* [1,4],
* ]
* Example 2:
* <p>
* Input: n = 1, k = 1
* Output: [[1]]
* <p>
* <p>
* 来源:力扣(LeetCode)
* 链接:https://leetcode-cn.com/problems/combinations
* 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
*/
public class Combinations {
// 注意剪枝
public List<List<Integer>> combine(int n, int k) {
List<List<Integer>> resList = new ArrayList<>();
List<Integer> res = new ArrayList<>();
dfs(1, k, n, res, resList);
return resList;
}
public void dfs(int pos, int k, int n, List<Integer> res, List<List<Integer>> resList) {
if (res.size() == k) {
resList.add(new ArrayList<>(res));
return;
}
if (pos > n) return;
// drill down
res.add(pos);
dfs(pos + 1, k, n, res, resList); // remain
res.remove(res.size() - 1);
if (k - res.size() <= n - pos) // 剪枝
dfs(pos + 1, k, n, res, resList); // drop
}
}