Non-Recursive solution with Python


  • 0
    S
    class Solution(object):
      def combinationSum(self, candidates, target):
        """
        :type candidates: List[int]
        :type target: int
        :rtype: List[List[int]]
        """
        i, s, size = 0, 0, len(candidates)
        indexList, tmpResult, result = [], [], []
        while i < size:
          tmpResult.append(candidates[i])
          indexList.append(i)
          s += candidates[i]
          
          if s >= target:
            if s == target:
              result.append(tmpResult[:])
              
            s -= tmpResult.pop()
            i = indexList.pop()+1
            while i == size and tmpResult:
              s -= tmpResult.pop()
              i = indexList.pop() + 1
        return result
    

  • 0

Log in to reply
 

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