-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy path69_combine.java
28 lines (26 loc) · 933 Bytes
/
69_combine.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
public class Solution {
/**
* @param n: Given the range of numbers
* @param k: Given the numbers of combinations
* @return: All the combinations of k numbers out of 1..n
*/
public List<List<Integer>> combine(int n, int k) {
// write your code here
if(n==0||k==0) return null;
List<List<Integer>> res = new ArrayList<>();
ArrayList<Integer> cur = new ArrayList<>();
getcombineList(n, k, 0, res, cur,1);
return res;
}
public void getcombineList(int n,int k,int x,List<List<Integer>> res,ArrayList<Integer> cur,int start ){
if(x==k){
res.add(new ArrayList<Integer>(cur));
return;
}
for(int i = start;i<=n;i++){
cur.add(i);
getcombineList(n, k, x+1, res, cur,i+1);
cur.remove(cur.size()-1);
}
}
}