给定一个正整数的数组和一个数sum,在数组抽取n个数相加等于sum,找出所有的可能,也可能无解。
每个数有两种状态取和不取,用0,1表示,数组中数字的组合可能有就 2**(length-1)种,如果是暴利破解就是2**length -1 次计算。
想想可以优化一下,求和的话可以先把数组排序,找到数组中比sum小的那一项a,然后用sum减去a得到b,那么可以看成是在a后面那一截数组中找到相加等于b的组合,依次这样递归下去。具体实现如下:
const array = [1,2,3,4,5,6,10,9,7,11,23]; const sum = 30; array.sort(function(a,b){ return a-b; }) console.log(array) const resultArray = []; function count(array,sum) { var length = array.length; if(array[0]>sum) { return false; } else if(array.indexOf(sum)!=-1) { resultArray.push(sum); console.log(resultArray); resultArray.pop(); } else{ for(var i=length-1;i>=0;i--) { if(array[i]<sum) { resultArray.push(array[i]); count(array.slice(0,i),sum-array[i]) resultArray.pop(); } } } } count(array,sum);
结果是
[ 23, 7 ]
[ 11, 10, 9 ]
[ 11, 9, 7, 3 ]
[ 11, 9, 6, 4 ]
[ 11, 9, 5, 4, 1 ]
[ 11, 9, 5, 3, 2 ]
[ 11, 9, 4, 3, 2, 1 ]
[ 11, 7, 6, 5, 1 ]
[ 11, 7, 6, 4, 2 ]
[ 11, 7, 6, 3, 2, 1 ]
[ 11, 7, 5, 4, 3 ]
[ 11, 6, 5, 4, 3, 1 ]
[ 10, 9, 7, 4 ]
[ 10, 9, 6, 5 ]
[ 10, 9, 5, 4, 2 ]
[ 10, 9, 5, 3, 2, 1 ]
[ 10, 7, 6, 5, 2 ]
[ 10, 7, 6, 4, 3 ]
[ 10, 7, 5, 4, 3, 1 ]
[ 10, 6, 5, 4, 3, 2 ]
[ 9, 7, 6, 5, 3 ]
[ 9, 7, 6, 4, 3, 1 ]
[ 9, 7, 5, 4, 3, 2 ]
[ 9, 6, 5, 4, 3, 2, 1 ]