17.?電話號(hào)碼的字母組合
給定一個(gè)僅包含數(shù)字2-9的字符串历恐,返回所有它能表示的字母組合。
給出數(shù)字到字母的映射如下(與電話按鍵相同)专筷。注意 1 不對(duì)應(yīng)任何字母弱贼。
示例:
輸入:"23"輸出:["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].
說明:
盡管上面的答案是按字典序排列的,但是你可以任意選擇答案輸出的順序磷蛹。
class Solution:
? ? dict_number = {'2': 'abc', '3': 'def', '4': 'ghi', '5': 'jkl', '6': 'mno', '7': 'pqrs', '8': 'tuv', '9': 'wxyz'}
? ? results = []
? ? def letterCombinations(self, digits):
? ? ? ? """
? ? ? ? :type digits: str
? ? ? ? :rtype: List[str]
? ? ? ? """
? ? ? ? self.results=[]
? ? ? ? lenght = len(digits)
? ? ? ? if lenght==0:
? ? ? ? ? ? return []
? ? ? ? self.getResult(digits, 0, [])
? ? ? ? return self.results
? ? def getResult(self, digits, index, result):
? ? ? ? if index==len(digits):
? ? ? ? ? ? self.results.append(''.join(result))
? ? ? ? ? ? return
? ? ? ? for i in range(len(self.dict_number[digits[index]])):
? ? ? ? ? ? result.append(self.dict_number[digits[index]][i])
? ? ? ? ? ? self.getResult(digits,index+1, result)
? ? ? ? ? ? result.pop(index)