Straightforward python version


  • -1
    H
    from functools import wraps
    def memo(func): 
        cache = {}
        @wraps(func)
        def wrap(*args):
            if args not in cache: 
                cache[args] = list(func(*args))
            return cache[args] 
        return wrap
        
    class Solution:
        # @param candidates, a list of integers
        # @param target, integer
        # @return a list of lists of integers
        def combinationSum(self, candidates, target):
            @memo
            def csumhelper(target):
                for c in candidates:
                    if target > c:
                        for candidate in csumhelper(target - c):
                            yield [c] + candidate
                    elif target == c:
                        yield [c]
    
            return [list(l) for l in set(tuple(sorted(c)) for c in csumhelper(target))]

Log in to reply
 

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