Skip to content

Commit 533069c

Browse files
author
Viktoria Jechsmayr - epunkt
committed
Update CombinationSum.js
1 parent a120254 commit 533069c

1 file changed

Lines changed: 0 additions & 40 deletions

File tree

‎Backtracking/CombinationSum.js‎

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +0,0 @@
1-
/**
2-
* @param {number[]} candidates
3-
* @param {number} target
4-
* @return {number[][]}
5-
*/
6-
var combinationSum = function(candidates, target) {
7-
var result = [];
8-
9-
if (candidates == null || candidates.length === 0) {
10-
return result;
11-
};
12-
13-
var current = [];
14-
candidates.sort();
15-
16-
combinationSumHelper(candidates, target, 0, current, result);
17-
18-
return result;
19-
};
20-
21-
function combinationSumHelper(candidates, target, j, curr, result){
22-
if(target === 0){
23-
var temp = curr.slice();
24-
result.push(temp);
25-
return;
26-
}
27-
28-
for(var i=j; i<candidates.length; i++){
29-
if(target < candidates[i]) {
30-
return;
31-
}
32-
curr.push(candidates[i]);
33-
combinationSumHelper(candidates, target - candidates[i], i, curr, result);
34-
curr.pop();
35-
}
36-
}
37-
38-
const candidates = [2,3,6,7];
39-
const target = 7;
40-
console.log(combinationSum(candidates, target));

0 commit comments

Comments
 (0)