JavaScript Solution


  • 0
    L
    function combinationSum(candidates, target) {
      candidates.sort((a, b) => a - b);
    
      var length = candidates.length;
      var res = [];
      search(0, [], target);
      return res;
    
      function search(idx, prefix, target) {
        if (target === 0) res.push(prefix.slice());
        if (idx === length) return;
        if (target <= 0) return;
    
        prefix.push(candidates[idx]);
        search(idx, prefix, target - candidates[idx]);
        prefix.pop();
        search(idx + 1, prefix, target);
      }
    };

  • 0
    C

    invoking the above function using this example
    combinationSum([3,8,4,10],15)

    gives an empty array


  • 0
    L

    I got [ [ 3, 3, 3, 3, 3 ], [ 3, 4, 4, 4 ], [ 3, 4, 8 ] ]

    https://repl.it/B6RC


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.