-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCombinations
More file actions
22 lines (22 loc) · 854 Bytes
/
Combinations
File metadata and controls
22 lines (22 loc) · 854 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public class Solution {
public ArrayList<ArrayList<Integer>> combine(int n, int k) {
// Note: The Solution object is instantiated only once and is reused by each test case.
ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
if(n <= 0 || k <= 0 || n<k) return result;
return helpler(1, n, k);
}
private ArrayList<ArrayList<Integer>> helpler(int min, int n, int k)
{
ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
if(k == 0) result.add(new ArrayList<Integer>());
else{
for(int i=min; i<=n-k+1; i++)
for(ArrayList<Integer> temp : helpler(i+1, n, k-1))
{
temp.add(0, i);
result.add(temp);
}
}
return result;
}
}