We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 8f8cd79 commit 8c6a50fCopy full SHA for 8c6a50f
combination-sum/hyer0705.ts
@@ -0,0 +1,21 @@
1
+function combinationSum(candidates: number[], target: number): number[][] {
2
+ const results: number[][] = [];
3
+
4
+ function backtrack(currentIndex: number, sum: number, selected: number[]) {
5
+ if (sum > target) return;
6
+ if (sum === target) {
7
+ results.push([...selected]);
8
+ return;
9
+ }
10
11
+ for (let i = currentIndex; i < candidates.length; i++) {
12
+ selected.push(candidates[i]);
13
+ backtrack(i, sum + candidates[i], selected);
14
+ selected.pop();
15
16
17
18
+ backtrack(0, 0, []);
19
20
+ return results;
21
+}
0 commit comments