Skip to content

Commit 5f6997b

Browse files
committed
combination-sum
1 parent 43c8be2 commit 5f6997b

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

combination-sum/sooooo-an.ts

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

0 commit comments

Comments
 (0)