// LeetCode指定调用方法 public List<List<Integer>> combinationSum3(int k, int n) { List<List<Integer>> result = new ArrayList<>();
backtracking(k, n, result, new ArrayList<>(), 1);
return result; }
// 回溯,记录当前candidates的开始位置起始元素 privatevoidbacktracking(int k, int n, List<List<Integer>> result, List<Integer> combinationSum3List, int t){
if (n == 0 || k == 0) { if (n == 0 && k == 0) { result.add(new ArrayList<>(combinationSum3List)); } return ; }
for (int i = t; i <= 9; i++) { if (i > n) { break; } combinationSum3List.add(i); backtracking(k - 1, n - i, result, combinationSum3List, i + 1); combinationSum3List.remove(combinationSum3List.size() - 1); }