-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.js
More file actions
30 lines (29 loc) · 833 Bytes
/
solution.js
File metadata and controls
30 lines (29 loc) · 833 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
30
/**
* @param {number[]} candidates
* @param {number} target
* @return {number[][]}
*/
var combinationSum2 = function(candidates, target) {
let res = []
backTrack(candidates, [], target, res)
return res.map(s => s.split(',').map(d => parseInt(d)))
};
var backTrack = function(candidates, temp, target, res) {
if (target === 0) {
temp.sort()
let s = temp.join(',')
if (res.findIndex(d => d === s) === -1) {
res.push(s)
}
} else {
candidates.forEach((c, i) => {
if (c <= target) {
let t = temp.slice(0),
temp_candidates = candidates.slice(0)
t.push(c)
temp_candidates.splice(i, 1)
backTrack(temp_candidates, t, target - c, res)
}
})
}
};