class Solution {
public List<List<Integer>> combinationSum3(int k, int n) {
List<List<Integer>> res = new ArrayList<>();
backtrack(k, n, 1, new ArrayList<>(), res);
return res;
}
private void backtrack(int k, int remain, int smallestCandidate, List<Integer> path, List<List<Integer>> res){
if(path.size() == k && remain == 0){
res.add(new ArrayList<>(path));
return;
}else if(path.size() == k || remain == 0){
return;
}
for(int i = smallestCandidate; i < 10; i++){
if(i > remain){
break;
}
path.add(i);
backtrack(k, remain - i, i + 1, path, res);
path.remove(path.size() - 1);
}
}
}