-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcombinationSumII.js
67 lines (66 loc) · 1.89 KB
/
combinationSumII.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/**
* https://leetcode.com/problems/combination-sum-ii/
* @param {number[]} candidates
* @param {number} target
* @return {number[][]}
*/
var combinationSum2 = function(candidates, target) {
if(candidates.length <= 0) return []
let res = []
candidates = candidates.sort((a,b) => a-b)
backTrack(0, target)
function backTrack(pos, total, arr=[]) {
if(total === 0) {
if(!isExist(res, arr)) {
res.push([].concat(arr))
}
return;
}
if(pos >= candidates.length) return;
backTrack(pos+1, total, arr)
if(candidates[pos] <= total) {
arr.push(candidates[pos])
backTrack(pos+1, total-candidates[pos], arr)
arr.pop()
}
}
return res
function isExist(target, arr) {
let str = arr.join('')
for(let i = 0; i < target.length; i++) {
let cur = target[i].join('')
if(cur === str) return true
}
}
};
/**
* 优化方案
* @param {number[]} candidates
* @param {number} target
* @return {number[][]}
*/
var combinationSum_2 = function(candidates, target) {
let res = []
// 先排序
candidates = candidates.sort((a,b) => a-b)
backTrack(0, target)
return res;
function backTrack(pos, total, arr=[]) {
if(total === 0) {
res.push([].concat(arr))
return;
}
for(let i = pos; i < candidates.length; i++) {
// 这一步是为了过滤已经有的组合
if(i > pos && candidates[i] === candidates[i-1]) continue;
if(candidates[i] <= total) {
arr.push(candidates[i])
backTrack(i+1, total-candidates[i], arr)
arr.pop()
}
}
}
};
console.log(combinationSum_2([1], 2))
console.log(combinationSum_2([1,2], 2))
console.log(combinationSum_2([10,1,2,7,6,1,5], 8))