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

Posted rosyyy

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了leetcode-mid-Linked list-17. Letter Combinations of a Phone Number相关的知识,希望对你有一定的参考价值。

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的主要内容,如果未能解决你的问题,请参考以下文章

Java list1=list2;list2=null ? list1=list2;list2.clear()?

Java list1=list2;list2=null ? list1=list2;list2.clear()?

Java list1=list2;list2=null ? list1=list2;list2.clear()?

list.addAll(list1),如果list改变,怎么让list1的值不跟着改变!

如何把两个list放到一个list中,然后在页面能获取到ID

List || Lists