JavaScript solution with backtracking


  • 2
    L
    function combinationSum2(candidates, target) {
      candidates.sort((a, b) => a - b);
      var buffer = [];
      var result = [];
      search(0, target);
      return result;
    
      function search(startIdx, target) {
        if (target === 0) return result.push(buffer.slice());
        if (target < 0) return;
        if (startIdx === candidates.length) return;
        buffer.push(candidates[startIdx]);
        search(startIdx, target - candidates[startIdx]);
        buffer.pop();
        search(startIdx + 1, target);
      }
    }

Log in to reply
 

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