Share my python solution, Optimized space complexity


  • 0
    class Solution(object):
        def combinationSum(self, candidates, target):
            candidates.sort()
            output = []
            
            def _helper(c, targ, res):
                if targ < 0:
                    return
                if targ == 0:
                    output.append(res[:])
                    return
                for i in range(c, len(candidates)):
                    res.append(candidates[i])
                    _helper(i, targ-candidates[i], res)
                    res.pop()
            _helper(0, target, [])
            return output
    

Log in to reply
 

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