1. 程式人生 > >根據手機上的按鍵將數字轉換成對應的可能字母組合

根據手機上的按鍵將數字轉換成對應的可能字母組合

end http 14. 輸出 info bubuko 成對 mage for

技術分享圖片

示例:

輸入:“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)

根據手機上的按鍵將數字轉換成對應的可能字母組合