Skip to content

Commit 417109e

Browse files
committed
combination-sum
1 parent 6c7ec07 commit 417109e

1 file changed

Lines changed: 22 additions & 0 deletions

File tree

combination-sum/nowrobin.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* @param {number[]} candidates
3+
* @param {number} target
4+
* @return {number[][]}
5+
*/
6+
function combinationSum(candidates, target) {
7+
var buffer = [];
8+
var result = [];
9+
search(0, target);
10+
return result;
11+
12+
function search(startIdx, target) {
13+
if (target === 0) return result.push(buffer.slice());
14+
if (target < 0) return;
15+
if (startIdx === candidates.length) return;
16+
buffer.push(candidates[startIdx]);
17+
search(startIdx, target - candidates[startIdx]);
18+
buffer.pop();
19+
search(startIdx + 1, target);
20+
}
21+
};
22+

0 commit comments

Comments
 (0)