forked from hijiangtao/LeetCode-with-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathres.js
More file actions
28 lines (23 loc) · 634 Bytes
/
res.js
File metadata and controls
28 lines (23 loc) · 634 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/**
* @param {number[]} candidates
* @param {number} target
* @return {number[][]}
*/
var combinationSum = function(candidates, target) {
candidates.sort((a, b) => b-a);
const res = [];
const calCombs = (candis, resArr, targ) => {
if (!candis.length) return ;
console.log(candis, resArr, targ);
const ele = candis[0];
if (targ === ele) {
res.push([...resArr, ele]);
} else if (targ - ele > 0) {
calCombs(candis.slice(), [...resArr, ele], targ-ele);
}
calCombs(candis.slice(1), [...resArr], targ);
}
calCombs(candidates, [], target);
// console.log(res);
return res;
};