1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| class Solution { List<List<Integer>> result = new ArrayList<>(); List<Integer> ans = new ArrayList<>(); public List<List<Integer>> combinationSum3(int k, int n) { int[] arr = new int[]{1,2,3,4,5,6,7,8,9}; dfs(arr, 0, k, n); return result; }
public void dfs(int[] arr, int begin, int k, int n){ if (n == 0 && k == 0){ result.add(new ArrayList<Integer>(ans)); return; }
if (k <= 0){ return; }
for (int i = begin; i < arr.length; i++) { ans.add(arr[i]); dfs(arr, i + 1, k - 1, n - arr[i]); ans.remove(ans.size() - 1); } } }
|