题目链接
回溯
class Solution {List<List<Integer>> res = new ArrayList<List<Integer>>();List<Integer> list = new ArrayList<Integer>();public List<List<Integer>> combinationSum3(int k, int n) {backtracking(k, n, 0, 1);return res;}public void backtracking(int k, int n, int sum, int start){if(list.size() == k && sum == n){res.add(new ArrayList<>(list));return ;}for(int i = start; i <= 9; i++){sum += i;list.add(i);backtracking(k, n, sum, i + 1);sum -= i;list.removeLast(); }}
}