题目链接:力扣
解题思路:
AC代码
class Solution {
List<Integer> tem = new ArrayList<>();
List<List<Integer>> result = new ArrayList<>();
public List<List<Integer>> combine(int n, int k) {
process(n, k, 1);
return result;
}
public void process(int n, int k, int cur) {
if (tem.size() + n - cur + 1 < k) {
return;
}
if (tem.size() == k) {
result.add(new ArrayList<>(tem));
return;
}
tem.add(cur);
process(n, k, cur + 1);
tem.remove(tem.size() - 1);
process(n, k, cur + 1);
}
}