-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCombinations.java
More file actions
102 lines (90 loc) · 2.41 KB
/
Copy pathCombinations.java
File metadata and controls
102 lines (90 loc) · 2.41 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
package Lists;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
/**
* @Author dekai.kong
* @description: 难度 Medium
* @create: 2018-12-23 19:24
* @from: https://leetcode.com/problems/combinations/
*
* Given two integers n and k, return all possible combinations of k numbers out of 1 ... n.
Example:
Input: n = 4, k = 2
Output:
[
[2,4],
[3,4],
[2,3],
[1,2],
[1,3],
[1,4],
]
**/
public class Combinations {
public static final List<Integer> INLIST = new ArrayList<>();
public Combinations() {
}
/**
* Runtime: 24 ms, faster than 53.60% of Java online submissions for Combinations.
* @param n
* @param k
* @return
* 使用递归recursive 运行时间中游水平
*/
public List<List<Integer>> combine2(int n, int k) {
List<List<Integer>> rst = new ArrayList<>();
if (n < 1 || k < 1 || k > n) {
return rst;
}
rst = helper(rst,n,k,1,new ArrayList<>());
return rst;
}
public List<List<Integer>> helper(List<List<Integer>> rst,int n,int k,int start,List<Integer> inlist){
if(inlist.size() == k){
rst.add(new ArrayList<>(inlist));
}else{
for (int i = start; i <= n; i++) {
inlist.add(i);
helper(rst,n,k,i+1,inlist);
inlist.remove(inlist.size()-1);
}
}
return rst;
}
/**
* leetcode 1ms
* Runtime: 1 ms, faster than 100.00% of Java online submissions for Combinations.
* @param n
* @param k
* @return
*/
public List<List<Integer>> combine(int n, int k) {
List<List<Integer>> ans = new ArrayList<>();
if (n < 1 || k < 1 || k > n) {
return ans;
}
makeCom(n, k, ans, new ArrayList<Integer>(), 0);
return ans;
}
public void makeCom(int n, int k, List<List<Integer>> ans, List<Integer> level, int pos) {
if (k == 0) {
ans.add(new ArrayList<>(level));
return;
}
for (int i = pos; i <= n - k; i++) {
level.add(i + 1);
makeCom(n, k - 1, ans, level, i + 1);
level.remove(level.size() - 1);
}
return;
}
@Test
public void test(){
// combine(4,2);
// combine(5,2);
// combine(4,3);
// combine(5,3);
combine(4,3);
}
}