解法
使用回溯算法
完成,
遇到的坑
使用JS完成这个算法,还需要DeepCopy
,使用JS的引用传递会导致list被改变
Code
/***
* 从给定有序数组中选取任意个数(可重复),使其和为给定值
*/
class Solution {
candidates: number[];
target: number;
res: number[][];
constructor(candidates: number[], target: number){
this.candidates = candidates
this.target = target
this.res = []
}
public combinationSum():number[][]{
this.helper(this.candidates, this.target, [],0)
return this.res
}
private helper(candidates: number[],target: number,list: number[],index: number){
if (target<0){
return;
}
if (target===0){
/***
* 为什么res最终是[[],[],[],[],[],[],[],[],[],[],[]]?
* 因为没有DeepCopy list,所以在循环中被pop和push更改了
*/
this.res.push(JSON.parse(JSON.stringify(list)))
return;
}
for (let i = index; i < candidates.length; i++) {
if (candidates[i]<=target){
let kk = target- candidates[i]
if (kk<0) continue;
list.push(candidates[i])
this.helper(candidates, kk,list,i)
list.pop()
}
}
}
}
const test = new Solution([1,2,3,4,5,6], 10)
const res = test.combinationSum()
console.log(res);