Created
October 9, 2019 09:07
-
-
Save Popalay/ad1a598da1574fb7be05aace345bdd03 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public List<List<Integer>> combinationSum(int[] candidates, int target) { | |
return new ArrayList<>(findComb(candidates, target)); | |
} | |
Set<List<Integer>> findComb(int[] candidates, int target){ | |
Set<List<Integer>> result = new HashSet<List<Integer>>(); | |
for(int i = 0; i < candidates.length; i++){ | |
if(candidates[i] == target){ | |
List<Integer> comb = new ArrayList<Integer>(); | |
comb.add(target); | |
result.add(comb); | |
} | |
if(candidates[i] < target){ | |
Set<List<Integer>> combs = findComb(candidates, target - candidates[i]); | |
for(List<Integer> comb : combs){ | |
comb.add(candidates[i]); | |
Collections.sort(comb); | |
} | |
result.addAll(combs); | |
} | |
} | |
return result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment