Skip to content

Instantly share code, notes, and snippets.

@Popalay
Created October 9, 2019 09:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Popalay/ad1a598da1574fb7be05aace345bdd03 to your computer and use it in GitHub Desktop.
Save Popalay/ad1a598da1574fb7be05aace345bdd03 to your computer and use it in GitHub Desktop.
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