Skip to content

Commit fd1b11b

Browse files
committed
combination-sum
1 parent 6092ac1 commit fd1b11b

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

combination-sum/chjung99.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution {
2+
Set<List<Integer>> combination = new HashSet<>();
3+
public List<List<Integer>> combinationSum(int[] candidates, int target) {
4+
Arrays.sort(candidates);
5+
findCombination(candidates, target, 0, 0, new ArrayList<>());
6+
return new ArrayList<>(combination);
7+
}
8+
9+
public void findCombination(int[] candidates, int target, int curIdx, int curSum, List<Integer> curList) {
10+
if (curSum == target) {
11+
combination.add(new ArrayList<>(curList));
12+
return;
13+
}
14+
15+
if (curSum > target) {
16+
return;
17+
}
18+
19+
for (int i = curIdx; i < candidates.length; i++){
20+
21+
curList.add(candidates[i]);
22+
findCombination(candidates, target, i, curSum + candidates[i], curList);
23+
curList.remove(curList.size()-1);
24+
}
25+
}
26+
}
27+
28+
29+

0 commit comments

Comments
 (0)