-
Notifications
You must be signed in to change notification settings - Fork 0
/
39.组合总数.js
47 lines (39 loc) · 1.01 KB
/
39.组合总数.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
/**
* @param {number[]} candidates
* @param {number} target
* @return {number[][]}
* https://leetcode.cn/problems/combination-sum/
*/
// 回溯
var combinationSum = function(candidates, target) {
const res = [];
candidates.sort((a, b) => a - b);
/**
*
*
* @param {*} preSum 上次计算的sum
* @param {*} path 组合
* @param {*} start 不用前面的数参与计算
* @return {*}
*/
const bt = (preSum, path, start) => {
if (preSum === 0) {
res.push([...path]);
return;
}
// 再减已无意义,candidate始终大于零, 条件中只存在正整数
if (preSum < 0) return;
for (let i = start; i < candidates.length; i++) {
// 配合前面的排序,此处才能剪枝
if (preSum - candidates[i] < 0) break;
path.push(candidates[i]);
// i不加1 因为可以重复使用当前
bt(preSum - candidates[i], path, i);
path.pop();
}
}
bt(target, [], 0);
console.log(res);
return res;
};
combinationSum([2,3,6,7], 7);