Skip to content

Commit 07c7559

Browse files
committed
Combination Sum 문제 풀이
1 parent e0d4bd9 commit 07c7559

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

combination-sum/ymir0804.java

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

0 commit comments

Comments
 (0)