码迷,mamicode.com
首页 > 其他好文 > 详细

leetcode-mid-Linked list-17. Letter Combinations of a Phone Number

时间:2019-06-03 22:14:19      阅读:162      评论:0      收藏:0      [点我收藏+]

标签:solution   res   dict   pre   str   object   obj   type   ring   

mycode  68.26%

class Solution(object):
    def letterCombinations(self, digits):
        """
        :type digits: str
        :rtype: List[str]
        """           
        if digits == "":
            return []
        dict = {2:[a,b,c],
                3:[d,e,f],
                4:[g,h,i],
                5:[j,k,l],
                6:[m,n,o],
                7:[p,q,r,s],
                8:[t,u,v],
                9:[w,x,y,z]
                }
        def dfs(index,temp):
            if index==self.len:
                self.res.append(temp)
                return 
            num = digits[index]
            s = dict[num]
            for i in s:
                dfs(index+1,temp+i)   
        self.res = []
        self.len = len(digits)
        dfs(0,"")
        return self.res

 

参考:

def letterCombinations(digits):
        def dfs(num, string):
            if num == length:
                res.append(string)
                return
            for letter in dict[digits[num]]:
                    dfs(num+1, string+letter)
        
        dict = {2:[a,b,c],
                3:[d,e,f],
                4:[g,h,i],
                5:[j,k,l],
                6:[m,n,o],
                7:[p,q,r,s],
                8:[t,u,v],
                9:[w,x,y,z]
                }
        res = []
        length = len(digits)
        dfs(0, ‘‘)
        return res

 

leetcode-mid-Linked list-17. Letter Combinations of a Phone Number

标签:solution   res   dict   pre   str   object   obj   type   ring   

原文地址:https://www.cnblogs.com/rosyYY/p/10969946.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!