Day24 回溯算法
我的思路:
套模板解题
解答:
class Solution {
public List<Integer> path = new ArrayList<Integer>();
public List<List<Integer>> res = new ArrayList();
public List<List<Integer>> combine(int n, int k) {
backtracing(n, 1, k);
return res;
}
public void backtracing(int n, int startIndex, int k) {
if(path.size() == k) {
res.add(new ArrayList(path));
return;
}
for(int i = startIndex; i <= n; i++) {
path.add(i);
backtracing(n, i + 1, k);
path.removeLast();
}
}
}