Python iterator solution

  • 0
    class Solution(object):
        def combinationSum(self, candidates, target):
            :type candidates: List[int]
            :type target: int
            :rtype: List[List[int]]
            def combs(c, t):
                if t == 0:
                    yield []
                elif t > 0 and c:
                    for tail in combs(c, t - c[0]):
                        yield [c[0]] + tail
                    for comb in combs(c[1:], t):
                        yield comb
            return list(combs(candidates, target))

Log in to reply

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