-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path17.py
40 lines (33 loc) · 976 Bytes
/
17.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# leetcode 17 电话号码的字母组合
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 = []
self.s = ""
def letterCombinations(self, digits: str) -> List[str]:
if len(digits) == 0:
return []
self.backTracking(digits, 0)
return self.result
def backTracking(self, digits, index):
if index == len(digits):
self.result.append(self.s)
return
digit = int(digits[index])
letters = self.letterMap[digit]
for i in range(len(letters)):
self.s += letters[i]
self.backTracking(digits, index+1)
self.s = self.s[:-1]