forked from forging2012/JavaArithmetic
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode77.java
More file actions
78 lines (57 loc) · 2.03 KB
/
LeetCode77.java
File metadata and controls
78 lines (57 loc) · 2.03 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
package LeetCode;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
public class LeetCode77 {
//https://leetcode-cn.com/problems/combinations/description/
/// 时间复杂度: O(n^k)
/// 空间复杂度: O(k)
private ArrayList<List<Integer>> res;
public List<List<Integer>> combine(int n, int k) {
res = new ArrayList<>();
if(n <= 0 || k <= 0 || k > n)
return res;
LinkedList<Integer> c = new LinkedList<>();
generateCombinations(n, k, 1, c);
return res;
}
// 求解C(n,k), 当前已经找到的组合存储在c中, 需要从start开始搜索新的元素
private void generateCombinations(int n, int k, int start, LinkedList<Integer> c){
if(c.size() == k){
res.add((List<Integer>)c.clone());
return;
}
for(int i = start ; i <= n ; i ++){
c.addLast(i);
generateCombinations(n, k, i + 1, c);
c.removeLast();
}
return;
}
// 求解C(n,k), 当前已经找到的组合存储在c中, 需要从start开始搜索新的元素
private void generateCombinations2(int n, int k, int start, LinkedList<Integer> c){
if(c.size() == k){
res.add((List<Integer>)c.clone());
return;
}
// 还有k - c.size()个空位, 所以, [i...n] 中至少要有 k - c.size() 个元素
// i最多为 n - (k - c.size()) + 1
// 这种优化叫做截枝
for(int i = start ; i <= n - (k - c.size()) + 1 ; i ++){
c.addLast(i);
generateCombinations2(n, k, i + 1, c);
c.removeLast();
}
return;
}
private static void printList(List<Integer> list){
for(Integer e: list)
System.out.print(e + " ");
System.out.println();
}
public static void main(String[] args) {
List<List<Integer>> res = (new LeetCode77()).combine(4, 2);
for(List<Integer> list: res)
printList(list);
}
}