Skip to content

Commit e03134f

Browse files
committed
combination-sum solution
1 parent 06ecedd commit e03134f

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

combination-sum/ohgyulim.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import java.util.*;
2+
3+
class Solution {
4+
List<List<Integer>> answer = new ArrayList<>();
5+
public List<List<Integer>> combinationSum(int[] candidates, int target) {
6+
Arrays.sort(candidates);
7+
recur(new ArrayList<Integer>(), candidates, target, 0, 0);
8+
return answer;
9+
}
10+
11+
public void recur(List<Integer> result, int[] candidates, int target, int sum, int index) {
12+
if (sum == target) {
13+
List<Integer> deepCopyRes = new ArrayList<>(result);
14+
answer.add(deepCopyRes);
15+
return;
16+
}
17+
for (int i = index; i < candidates.length; i++) {
18+
if (sum + candidates[i] <= target) {
19+
result.add(candidates[i]);
20+
recur(result, candidates, target, sum + candidates[i], i);
21+
result.remove(Integer.valueOf(candidates[i]));
22+
}
23+
}
24+
}
25+
}

0 commit comments

Comments
 (0)