forked from hijiangtao/LeetCode-with-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathres.js
31 lines (26 loc) · 733 Bytes
/
res.js
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
29
30
31
/**
* @param {number[]} candidates
* @param {number} target
* @return {number[][]}
*/
var combinationSum2 = function(candidates, target) {
candidates.sort((a, b) => b-a);
const res = [];
const calCombs = (candis, resArr, targ) => {
if (!candis.length) return ;
const ele = candis[0];
if (targ === ele) {
res.push([...resArr, ele]);
} else if (targ - ele > 0) {
calCombs(candis.slice(1), [...resArr, ele], targ-ele);
}
let sliceIndex = 0;
while (sliceIndex < candis.length && candis[0] === candis[sliceIndex]) {
sliceIndex++;
}
calCombs(candis.slice(sliceIndex), [...resArr], targ);
}
calCombs(candidates, [], target);
// console.log(res);
return res;
};