首页 > 技术文章 > leetcode Letter Combinations of a Phone Number python

allenhaozi 2015-11-21 00:00 原文

class Solution(object):
    def letterCombinations(self, digits):
        """
        :type digits: str
        :rtype: List[str]
        """
        if len(digits) <= 0:
            return list()
        alpha = ["","1","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"]
        res=[]
        word=[]
        def dfs(cur):
            if cur >= len(digits):
                res.append(''.join(word))
            else:
                for x in alpha[int(digits[cur])-(int)('0')]:
                    word.append(x)
                    dfs(cur+1)
                    word.pop()
        dfs(0)
        
        return res

@link http://blog.csdn.net/hcbbt/article/details/44061179

推荐阅读