-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path39.js
82 lines (78 loc) · 1.59 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/**
* @param {number[]} candidates
* @param {number} target
* @return {number[][]}
*/
var combinationSum = function(candidates, target) {
candidates = candidates.sort((a, b) => a - b);
return com(target, 0);
function com(target, i) {
let res = [];
while (candidates[i] <= target) {
if (candidates[i] <= target - candidates[i]) {
let result = com(target - candidates[i], i);
res.push(
...result.map(item => {
item.push(candidates[i]);
return item;
})
);
} else if (candidates[i] === target) {
res.push([candidates[i]]);
}
i++;
}
return res;
}
};
let a = [2, 3, 6, 7];
let t = 7;
a = [2, 3, 4, 5, 6, 7, 8, 9, 10];
t = 10;
a = [8, 7, 4, 3];
t = 8;
a = [10,2,7,6,1,5];
t = 10
// console.log(combinationSum(a, t));
combinationSum(a, t).forEach(item => {
console.log(item.join(","));
});
debugger;
// [2,3,4,5,6,7,8,9,10] 10
// [
// [2,8],
// [2,6],
// [2,4],
// [2,2]
// [4]
// [3,3],
// [6]
// [3,5],
// [2,3],
// [5],
// [4,4],
// [2,2],
// [4]
// [8]
// [3,7],
// [2,5],
// [2,3],
// [5]
// [3,4]
// [2,2],
// [4],
// [7],
// [4,6],
// [2,4],
// [2,2],
// [4],
// [3,3],
// [6]
// [5,5],
// // [6,4],
// // [7,3],
// // [8,2],
// [10],
// ]
// [2,3,6,7] 7
[[2, 5], [2, 3][(3, 4)], [2, 2][7]];