-
Notifications
You must be signed in to change notification settings - Fork 10
/
Combinations.java
47 lines (39 loc) · 1.21 KB
/
Combinations.java
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
/*
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n.
For example,
If n = 4 and k = 2, a solution is:
[
[2,4],
[3,4],
[2,3],
[1,2],
[1,3],
[1,4],
]
*/
public class Solution {
public ArrayList<ArrayList<Integer>> combine(int n, int k) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
if (n <= 0 || k <= 0 || n < k) return result;
for (int i = 1; i <= n; i++) {
ArrayList<Integer> init = new ArrayList<Integer>();
init.add(i);
helper(init, n, k, result);
}
return result;
}
public void helper(ArrayList<Integer> cur, int n, int k, ArrayList<ArrayList<Integer>> result) {
if (cur.size() == k) {
result.add(cur);
return;
}
int curMax = cur.get(cur.size() - 1);
while (curMax < n) {
ArrayList<Integer> res = new ArrayList<Integer>(cur);
res.add(++curMax);
helper(res, n, k, result);
}
}
}