public List<List<Integer>> combin3(int k,int n){
  List<List<Integer>> res = new  ArrayList<List<Integer>>();
  backtrack(res,new ArrayList<Integer>(),n,n,k,1);
   return res; 
}

public void backtrack(List<List<Integer>> res, List<Integer> tempList, int remain, int n, int k ,int start){
if(remain<0) return;
else if(remain==0&&tempList.size()==k) res.add(new ArrayList<>(tempList));

else{
for(int i=start;i<=9;i++){
if(i>start&&i==i-1) continue;
tempList.add(i);
backtrack(res,tempList,remain-i,n,k,i+1);
tempList.remove(tempList.size()-1);
 }
}
}

results matching ""

    No results matching ""