2023.6.26
这个题相对来说还是比较简单的,也没有什么剪枝操作,就是一个简单的组合问题
class Solution:
def __init__(self):
self.letterMap = [
"", # 0
"", # 1
"abc", # 2
"def", # 3
"ghi", # 4
"jkl", # 5
"mno", # 6
"pqrs", # 7
"tuv", # 8
"wxyz" # 9
]
self.result = []
def letterCombinations(self, digits: str) -> List[str]:
self.letterMap = [
"", # 0
"", # 1
"abc", # 2
"def", # 3
"ghi", # 4
"jkl", # 5
"mno", # 6
"pqrs", # 7
"tuv", # 8
"wxyz" # 9
]
self.result = []
self.digits = digits
if not self.digits:
return []
n = len(digits)
self.backtracking(n, 0, '')
return self.result
def backtracking(self, n, index, path):
# n是digits的长度
if len(path) == n:
self.result.append(path)
return
for i in self.letterMap[int(self.digits[index])]:
path += i
self.backtracking(n, index + 1, path)
path = path[:-1]