Finding all possible combinations of numbers to reach a given sum

前端 未结 30 3028
一个人的身影
一个人的身影 2020-11-21 06:39

How would you go about testing all possible combinations of additions from a given set N of numbers so they add up to a given final number?

A brief exam

30条回答
  •  伪装坚强ぢ
    2020-11-21 07:10

    A Javascript version:

    function subsetSum(numbers, target, partial) {
      var s, n, remaining;
    
      partial = partial || [];
    
      // sum partial
      s = partial.reduce(function (a, b) {
        return a + b;
      }, 0);
    
      // check if the partial sum is equals to target
      if (s === target) {
        console.log("%s=%s", partial.join("+"), target)
      }
    
      if (s >= target) {
        return;  // if we reach the number why bother to continue
      }
    
      for (var i = 0; i < numbers.length; i++) {
        n = numbers[i];
        remaining = numbers.slice(i + 1);
        subsetSum(remaining, target, partial.concat([n]));
      }
    }
    
    subsetSum([3,9,8,4,5,7,10],15);
    
    // output:
    // 3+8+4=15
    // 3+5+7=15
    // 8+7=15
    // 5+10=15

提交回复
热议问题