Skip to content

Commit 9303e78

Browse files
authored
solve combintaion sum
1 parent f531850 commit 9303e78

1 file changed

Lines changed: 20 additions & 0 deletions

File tree

combination-sum/grapefruit13.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
function combinationSum(candidates: number[], target: number): number[][] {
2+
const answer: number[][] = [];
3+
4+
function dfs(index: number, path: number[], sum: number) {
5+
if (sum === target) {
6+
answer.push([...path]);
7+
return;
8+
}
9+
if (sum > target) return;
10+
11+
for (let i = index; i < candidates.length; i++) {
12+
path.push(candidates[i]);
13+
dfs(i, path, sum + candidates[i]);
14+
path.pop();
15+
}
16+
}
17+
18+
dfs(0, [], 0);
19+
return answer;
20+
}

0 commit comments

Comments
 (0)