216.组合总和Ⅲ
class Solution {
public List<List<Integer>> combinationSum3(int k, int n) {
List<List<Integer>> res=new ArrayList<>();
Deque<Integer> path=new ArrayDeque<>();
if(n<1){
return res;
}
backtrack(k,n,res,path,1);
return res;
}
public void backtrack(int k,int n,List<List<Integer>> res,Deque<Integer> path,int begin){
if(n<0){
return;
}
if(n==0&&path.size()==k){
res.add(new ArrayList(path));
return;
}
for(int i=begin;i<=9;i++){
if(path.size()>k){
break;
}
path.addLast(i);
backtrack(k, n-i, res, path, i+1);
path.removeLast();
}
}
}