题目如下:
题目
这道题思路如下,想象手机的用户逐一输入数字,我们用一个列表res来记录之前输入结果,用另一个数组来对当前的输入结果进行更新。
参考代码如下:
class Solution:
def letterCombinations(self, digits):
"""
:type digits: str
:rtype: List[str]
"""
if not digits:
return []
type_dic = {
"2": "abc",
"3": "def",
"4": "ghi",
"5": "jkl",
"6": "mno",
"7": "pqrs",
"8": "tuv",
"9": "wxyz"
}
res = [""]
for d in digits:
temp = []
if d in type_dic.keys():
for c in type_dic[d]:
for r in res:
temp.append(r + c)
res = temp
return res
ps:如果您有好的建议,欢迎交流 :-D,也欢迎访问我的个人博客:tundrazone.com