LeetCode(17)Letter Combinations of a Phone Number

Posted 你瞅啥

tags:

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

题目如下:

Python代码:

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\'}
        result = []
        self.helper(digits,dic,0,"",result)
        return result
    
    def helper(self,digits,dic,index,temp,result):
        if index==len(digits):
            result.append(temp)
        else:
            s = dic[digits[index]]
            for i in s:
                temp += i
                self.helper(digits,dic,index+1,temp,result)
                temp = temp[:-1]

 

以上是关于LeetCode(17)Letter Combinations of a Phone Number的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode算法题python解法:17. Letter Combinations of a Phone Number

Leetcode 17.——Letter Combinations of a Phone Number

LeetCode-17-Letter Combinations of a Phone Number

LeetCode17. Letter Combinations of a Phone Number

LeetCode17:Letter Combinations of a Phone Number

[LeetCode] 17. Letter Combinations of a Phone Number