37 lines
902 B
Java
37 lines
902 B
Java
/*
|
|
* @lc app=leetcode id=77 lang=java
|
|
*
|
|
* [77] Combinations
|
|
*/
|
|
|
|
// @lc code=start
|
|
class Solution {
|
|
public List<List<Integer>> combine(int n, int k) {
|
|
List<List<Integer>> r = new ArrayList<List<Integer>>();
|
|
List<Integer> rt = new ArrayList<Integer>();
|
|
dfsIterate(r,rt, 1, n, k);
|
|
return r;
|
|
}
|
|
|
|
private void dfsIterate(List<List<Integer>> r, List<Integer> list, int c, int n, int k) {
|
|
if(list.size() == k) {
|
|
r.add(shallowCopy(list));
|
|
return;
|
|
}
|
|
if(c > n) {
|
|
return;
|
|
}
|
|
for(int i = c; i<=n; i++) {
|
|
List<Integer> p = shallowCopy(list);
|
|
p.add(i);
|
|
dfsIterate(r, p, i+1, n, k);
|
|
}
|
|
}
|
|
|
|
private List<Integer> shallowCopy(List<Integer> list) {
|
|
return new ArrayList<>(list);
|
|
}
|
|
}
|
|
// @lc code=end
|
|
|