function findCombinations(arr, target, start = 0, current = [], result = []) {
if (target == current.reduce((l, e) => l + e, 0)) {
result.push([...current])
return result
}
if (target < current.reduce((l, e) => l + e, 0)) {
return
}
for (let i = start; i < arr.length; i++) {
current.push(arr[i])
findCombinations(arr, target, i, current, result)
current.pop()
}
return result
}
let arr = [3, 4, 12, 6];
let num = 15;
console.log(findCombinations(arr, num));
js 算法找零问题
最新推荐文章于 2024-11-12 17:31:26 发布