17. 电话号码的字母组合

class Solution(object): #回溯法
    def letterCombinations(self, digits):
        """
        :type digits: str
        :rtype: List[str]
        """
        if not digits:
            return []
        dic = {'2':'abc','3':'def','4':'ghi','5':'jkl','6':'mno','7':'pqrs','8':'tuv','9':'wxyz'}
        temp = []
        for i in digits:
            if i in dic:
                temp.append(dic[i])
        result = []
        def dfs(s,temp):
            if not temp:
                result.append(s)
                return
            for i in temp[0]:
                dfs(s+i,temp[1:]) 
        dfs('',temp)
        return result

  

posted @ 2020-04-16 10:28  USTC丶ZCC  阅读(125)  评论(0编辑  收藏  举报