Python 88ms solution


  • 0
    4
    class Solution:
        # @param {integer[]} candidates
        # @param {integer} target
        # @return {integer[][]}
        def combinationSum(self, candidates, target):
            candidates.sort()
            ans = []
            self.help(candidates, target, [], 0, ans)
            return ans
    
        def help(self, a, target, cur, s, ans):
            if target == 0:
                ans.append(cur[:])
            if target < 0 or a[s] > target:
                return
            while s < len(a):
                cur.append(a[s])
                r = self.help(a, target - a[s], cur, s, ans)
                cur.pop()
                if target - a[s] < 0:
                    break
                s += 1

Log in to reply
 

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