-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCombinations.java
More file actions
21 lines (19 loc) · 638 Bytes
/
Copy pathCombinations.java
File metadata and controls
21 lines (19 loc) · 638 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution {
public List<List<Integer>> combine(int n, int k) {
if(k <= 0) return Collections.emptyList();
List<List<Integer>> result = new ArrayList<>();
ch(result, new ArrayList<>(), 1, k, n);
return result;
}
private void ch(List<List<Integer>> result, List<Integer> sofar, int s, int k, int n) {
if(sofar.size() == k) {
result.add(new ArrayList<>(sofar));
return;
}
for(int i = s; i <= n ; i++){
sofar.add(i);
ch(result, sofar, i+1, k, n);
sofar.remove(sofar.size()-1);
}
}
}