-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathCombinations.java
More file actions
122 lines (111 loc) · 3.38 KB
/
Copy pathCombinations.java
File metadata and controls
122 lines (111 loc) · 3.38 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/**
* <p>给定两个整数 <code>n</code> 和 <code>k</code>,返回范围 <code>[1, n]</code> 中所有可能的 <code>k</code> 个数的组合。</p>
*
* <p>你可以按 <strong>任何顺序</strong> 返回答案。</p>
*
* <p> </p>
*
* <p><strong>示例 1:</strong></p>
*
* <pre>
* <strong>输入:</strong>n = 4, k = 2
* <strong>输出:</strong>
* [
* [2,4],
* [3,4],
* [2,3],
* [1,2],
* [1,3],
* [1,4],
* ]</pre>
*
* <p><strong>示例 2:</strong></p>
*
* <pre>
* <strong>输入:</strong>n = 1, k = 1
* <strong>输出:</strong>[[1]]</pre>
*
* <p> </p>
*
* <p><strong>提示:</strong></p>
*
* <ul>
* <li><code>1 <= n <= 20</code></li>
* <li><code>1 <= k <= n</code></li>
* </ul>
* <div><div>Related Topics</div><div><li>数组</li><li>回溯</li></div></div><br><div><li>👍 783</li><li>👎 0</li></div>
*/
package leetcode3;
import java.util.*;
public class Combinations {
public static void main(String[] args) {
Solution solution = new Combinations().new Solution();
}
/**
* 最优解法
*/
class Solution {
public List<List<Integer>> combine(int n, int k) {
List<List<Integer>> res = new ArrayList<>();
Integer[] arr = new Integer[k];
solve(res, arr, n, 0);
return res;
}
private void solve(List<List<Integer>> res, Integer[] arr, int n, int level) {
if (level >= arr.length) {
res.add(new ArrayList<>(Arrays.asList(arr)));
return;
}
int val = level == 0 ? 1 : arr[level - 1] + 1;
for (; val <= n; val++) {
arr[level] = val;
solve(res, arr, n, level + 1);
}
}
}
/**
* 递归,遍历出所有list并返回
*/
class Solution2 {
public List<List<Integer>> combine(int n, int k) {
List<List<Integer>> res = new ArrayList<>();
solve(res, new ArrayList<>(), n, k, 1);
return res;
}
private void solve(List<List<Integer>> res, List<Integer> list, int n, int k, int i) {
if (list.size() == k) {
res.add(list);
return;
}
for (; i <= n - k + list.size() + 1; i++) {
List<Integer> next = new ArrayList<>(list);
next.add(i);
solve(res, next, n, k, i + 1);
}
}
}
/**
* 递归,使用stack求出所有解,而后转为list输出
* 剪枝优化:剩下数字不足以填充stack时,停止遍历
* 备注:剪枝优化前,solution1耗时少;剪枝优化后,两种方法耗时等同
*/
class Solution3 {
public List<List<Integer>> combine(int n, int k) {
List<List<Integer>> res = new ArrayList<>();
Deque<Integer> stack = new ArrayDeque<>(k);
resolve(n, k, 1, stack, res);
return res;
}
private void resolve(int n, int k, int start, Deque<Integer> stack, List<List<Integer>> res) {
if (stack.size() == k) {
res.add(new ArrayList<>(stack));
return;
}
for (int i = start; i <= n - (k - stack.size()) + 1; i++) {
stack.addLast(i);
resolve(n, k, i + 1, stack, res);
stack.removeLast();
}
}
}
}