Skip to content

Commit c8d52f3

Browse files
committed
WEEK 3 Solutions
1 parent 6e34e71 commit c8d52f3

1 file changed

Lines changed: 24 additions & 0 deletions

File tree

combination-sum/juhui-jeong.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
public List<List<Integer>> combinationSum(int[] candidates, int target) {
3+
// dfs & 백트래킹
4+
List<List<Integer>> resultList = new ArrayList<>();
5+
dfs(candidates, target, 0, new ArrayList<>(), resultList);
6+
return resultList;
7+
}
8+
private void dfs(int[] candidates, int target, int start, List<Integer> path, List<List<Integer>> resultList) {
9+
if (target == 0) {
10+
resultList.add(new ArrayList<>(path));
11+
return;
12+
}
13+
14+
if (target < 0) {
15+
return;
16+
}
17+
18+
for(int i = start; i < candidates.length; i++) {
19+
path.add(candidates[i]);
20+
dfs(candidates, target - candidates[i], i, path, resultList);
21+
path.remove(path.size() -1);
22+
}
23+
}
24+
}

0 commit comments

Comments
 (0)