Advertisement
Guest User

Untitled

a guest
Mar 25th, 2019
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.10 KB | None | 0 0
  1. class Solution {
  2. public List<List<Integer>> combinationSum(int[] candidates, int target) {
  3. List<List<Integer>> answer = new ArrayList();
  4.  
  5. if (candidates.length == 0) {
  6. return answer;
  7. }
  8.  
  9. Arrays.sort(candidates);
  10. int currSum = 0;
  11. combinationSumBackTrack(answer, new ArrayList<Integer>(), candidates, target, currSum, 0);
  12. return answer;
  13. }
  14.  
  15. private void combinationSumBackTrack(List<List<Integer>> answer, List<Integer> currentStore, int[] candidates, int target, int currSum, int currentIndex) {
  16.  
  17. if (currSum == target) {
  18. answer.add(new ArrayList<>(currentStore));
  19. return;
  20. }
  21.  
  22. for (int i = currentIndex; i < candidates.length; i++) {
  23. if (currSum + candidates[i] <= target) {
  24. currentStore.add(candidates[i]);
  25. combinationSumBackTrack(answer, currentStore, candidates, target, currSum + candidates[i], i);
  26. currentStore.remove(Integer.valueOf(candidates[i]));
  27. }
  28. }
  29. }
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement