Guest User

Untitled

a guest
Mar 18th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.77 KB | None | 0 0
  1. class Solution {
  2. public List<List<Integer>> combinationSum3(int k, int n) {
  3. List<List<Integer>> res = new ArrayList<>();
  4. backtracking(res, new ArrayList<Integer>(), 1, k, 0, n);
  5. return res;
  6. }
  7.  
  8. private void backtracking(List<List<Integer>> res, List<Integer> path, int index, int k, int curr, int target) {
  9. if (path.size() == k) {
  10. if (curr == target) {
  11. res.add(new ArrayList<>(path));
  12. }
  13.  
  14. return;
  15. }
  16.  
  17. if (curr > target || path.size() > k) return;
  18.  
  19. for (int i = index; i <= 9; i++) {
  20. path.add(i);
  21. backtracking(res, path, i + 1, k, curr + i, target);
  22. path.remove(path.size() - 1);
  23. }
  24. }
  25. }
Add Comment
Please, Sign In to add comment