-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathCombinations.java
More file actions
26 lines (22 loc) · 811 Bytes
/
Combinations.java
File metadata and controls
26 lines (22 loc) · 811 Bytes
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
public class Solution {
public ArrayList<ArrayList<Integer>> combine(int n, int k) {
// Start typing your Java solution below
// DO NOT write main() function
ArrayList<Integer> col = new ArrayList<Integer>();
ArrayList<ArrayList<Integer>> re = new ArrayList<ArrayList<Integer>>();
getlist(n,k,col,re,0);
return re;
}
public void getlist(int n, int k, ArrayList<Integer> col, ArrayList<ArrayList<Integer>> re, int start){
if(col.size()==k){
ArrayList<Integer> t = new ArrayList<Integer>(col);
re.add(t);
return;
}
for(int i=start; i<n; i++){
col.add(i+1);
getlist(n,k,col,re,i+1);
col.remove(col.size()-1);
}
}
}