回溯
class Solution {
List<List<Integer>> res = new ArrayList<List<Integer>>();
List<Integer> list = new ArrayList<Integer>();
public List<List<Integer>> combine(int n, int k) {
backtracking(n, k, 1);
return res;
}
public void backtracking(int n, int k, int startIndex){
if(list.size() == k){
res.add(new ArrayList<>(list));
return;
}
for(int i = startIndex; i <= n; i++){
list.add(i);
backtracking(n, k, i + 1);
list.removeLast();
}
}
}