Skip to content

Commit 8769dc0

Browse files
committed
Time: 7 ms (19.85%), Space: 62.3 MB (13.8%) - LeetHub
source:c5f55d8a67db97d956ea0917b3896f10405d55d2
1 parent 8135236 commit 8769dc0

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* @param {number[]} candidates
3+
* @param {number} target
4+
* @return {number[][]}
5+
*/
6+
var combinationSum = function(candidates, target) {
7+
const ans = [];
8+
candidates.sort((a,b)=> a-b);
9+
10+
dfs(0,0,[]);
11+
12+
return ans;
13+
14+
function dfs(idx, sum, arr){
15+
const current = sum + candidates[idx];
16+
const currentArr = [...arr, candidates[idx]];
17+
18+
if(current > target) return;
19+
20+
if(current === target) {
21+
ans.push(currentArr);
22+
return;
23+
};
24+
25+
dfs(idx, current,currentArr);
26+
if(idx +1 < candidates.length) dfs(idx+1, sum, arr);
27+
28+
}
29+
30+
};

0 commit comments

Comments
 (0)