根据手机上的按键将数字转换成对应的可能字母组合

根据手机上的按键将数字转换成对应的可能字母组合

示例:

输入:“34”

输出:["dg","dh","di","eg","eh","ei","fg","fh","fi"]

Python解决方案:

class Solution(object):
    def letterCombinations(self, digits):
        """
        :type digits: str
        :rtype: List[str]
        """
        if not digits:
            return []
        self.num2alpha = {"2":"abc","3":"def","4":"ghi","5":"jkl","6":"mno","7":"pqrs","8":"tuv","9":"wxyz"}
        self.out = []
        self.letters(digits)
        return self.out
    def letters(self,digits,i=0,s=""):
        if i == len(digits):
            self.out.append(s)
            return
        for j in self.num2alpha[digits[i]]:
            self.letters(digits,i+1,s+j)

 

根据手机上的按键将数字转换成对应的可能字母组合

上一篇:H5页面移动端兼容性测试


下一篇:java网络编程