Skip to content

Commit 8f783f7

Browse files
committed
Add solution.
1 parent ee66816 commit 8f783f7

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

Combination.java

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
public class Solution {
2+
public ArrayList<ArrayList<Integer>> combine(int n, int k) {
3+
// Start typing your Java solution below
4+
// DO NOT write main() function
5+
return findCombine(n, k);
6+
7+
}
8+
9+
public ArrayList<ArrayList<Integer>> findCombine(int n, int k){
10+
ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
11+
if (k == 1){
12+
for (int i = 1; i <=n; i++){
13+
ArrayList<Integer> temp = new ArrayList<Integer>();
14+
temp.add(i);
15+
result.add(temp);
16+
}
17+
return result;
18+
}
19+
else{
20+
for (int m = n; m >0; m--){
21+
for (ArrayList<Integer> al : findCombine(m - 1, k - 1)){
22+
al.add(m);
23+
result.add(al);
24+
}
25+
}
26+
return result;
27+
}
28+
29+
}
30+
}

0 commit comments

Comments
 (0)