generator python


  • 0
    M

    class Solution:
    def letterCombinations(self, digits):
    """
    :type digits: str
    :rtype: List[str]
    """

        self.mapping = {
            '2': 'abc',
            '3': 'def',
            '4': 'ghi',
            '5': 'jkl',
            '6': 'mno',
            '7': 'pqrs',
            '8': 'tuv',
            '9': 'wxyz'
        }
        result = list(self.letterCombinationsCore(digits))
        return result
    
    def letterCombinationsCore(self, digits):
        if len(digits) == 1:
            for x in self.mapping[digits]:
                yield x
        else:
            for x in self.mapping[digits[0]]:
                for y in self.letterCombinationsCore(digits[1:]):
                    yield x+y

Log in to reply
 

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