My recursive Python solution


  • 0

    The solution is:

        if k == 1 and n <= 9:
            return [] if n <= m else [[n]]
        result = []
        for i in range(m + 1, 10):
            suffix = self.combinationSum3(k - 1, n - i, i)
            result += [[i] + _ for _ in suffix] if suffix else []
        return result
    

    I think the code:

        result = []
        for i in range(m + 1, 10):
            suffix = self.combinationSum3(k - 1, n - i, i)
            result += [[i] + _ for _ in suffix] if suffix else []
        return result
    

    can be replace by one line code.


Log in to reply
 

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