-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path040.js
More file actions
29 lines (26 loc) · 723 Bytes
/
040.js
File metadata and controls
29 lines (26 loc) · 723 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
29
/**
* @param {number[]} candidates
* @param {number} target
* @return {number[][]}
*/
var combinationSum2 = function(candidates, target) {
candidates.sort((a, b) => a - b);
let results = [];
let result = [];
const findResults = (startIndex, target) => {
if (target === 0) {
results.push(result.slice(0));
return;
}
if (target < 0) return;
if (startIndex === candidates.length) return;
for (let i = startIndex; i < candidates.length; ++i) {
if (i > startIndex && candidates[i] === candidates[i - 1]) continue;
result.push(candidates[i]);
findResults(i + 1, target - candidates[i]);
result.pop();
}
};
findResults(0, target);
return results;
};