@@ -282,61 +282,74 @@ class Solution {
282
282
```
283
283
284
284
## Python
285
-
286
- ``` Python
285
+ ** 回溯 **
286
+ ``` python3
287
287
class Solution :
288
- ans = []
289
- s = ' '
290
- letterMap = {
291
- ' 2' : ' abc' ,
292
- ' 3' : ' def' ,
293
- ' 4' : ' ghi' ,
294
- ' 5' : ' jkl' ,
295
- ' 6' : ' mno' ,
296
- ' 7' : ' pqrs' ,
297
- ' 8' : ' tuv' ,
298
- ' 9' : ' wxyz'
299
- }
288
+ def __init__ (self ):
289
+ self .answers: List[str ] = []
290
+ self .answer: str = ' '
291
+ self .letter_map = {
292
+ ' 2' : ' abc' ,
293
+ ' 3' : ' def' ,
294
+ ' 4' : ' ghi' ,
295
+ ' 5' : ' jkl' ,
296
+ ' 6' : ' mno' ,
297
+ ' 7' : ' pqrs' ,
298
+ ' 8' : ' tuv' ,
299
+ ' 9' : ' wxyz'
300
+ }
300
301
301
- def letterCombinations (self , digits ):
302
- self .ans.clear()
303
- if digits == ' ' :
304
- return self .ans
302
+ def letterCombinations (self , digits : str ) -> List[str ]:
303
+ self .answers.clear()
304
+ if not digits: return []
305
305
self .backtracking(digits, 0 )
306
- return self .ans
307
-
308
- def backtracking (self , digits , index ):
309
- if index == len (digits):
310
- self .ans.append(self .s)
311
- return
312
- else :
313
- letters = self .letterMap[digits[index]] # 取出数字对应的字符集
314
- for letter in letters:
315
- self .s = self .s + letter # 处理
316
- self .backtracking(digits, index + 1 )
317
- self .s = self .s[:- 1 ] # 回溯
306
+ return self .answers
307
+
308
+ def backtracking (self , digits : str , index : int ) -> None :
309
+ # 回溯函数没有返回值
310
+ # Base Case
311
+ if index == len (digits): # 当遍历穷尽后的下一层时
312
+ self .answers.append(self .answer)
313
+ return
314
+ # 单层递归逻辑
315
+ letters: str = self .letter_map[digits[index]]
316
+ for letter in letters:
317
+ self .answer += letter # 处理
318
+ self .backtracking(digits, index + 1 ) # 递归至下一层
319
+ self .answer = self .answer[:- 1 ] # 回溯
318
320
```
319
-
320
- python3:
321
-
322
- ``` py
321
+ ** 回溯简化**
322
+ ``` python3
323
323
class Solution :
324
+ def __init__ (self ):
325
+ self .answers: List[str ] = []
326
+ self .letter_map = {
327
+ ' 2' : ' abc' ,
328
+ ' 3' : ' def' ,
329
+ ' 4' : ' ghi' ,
330
+ ' 5' : ' jkl' ,
331
+ ' 6' : ' mno' ,
332
+ ' 7' : ' pqrs' ,
333
+ ' 8' : ' tuv' ,
334
+ ' 9' : ' wxyz'
335
+ }
336
+
324
337
def letterCombinations (self , digits : str ) -> List[str ]:
325
- res = []
326
- s = " "
327
- letterMap = [ " " , " " , " abc " , " def " , " ghi " , " jkl " , " mno " , " pqrs " , " tuv " , " wxyz " ]
328
- if not len (digits): return res
329
- def backtrack ( digits , index , s ):
330
- if index == len (digits) :
331
- return res.append(s)
332
- digit = int (digits[index]) # 将index指向的数字转为int
333
- letters = letterMap[digit] # 取数字对应的字符集
334
- for i in range ( len (letters)):
335
- s += letters[i]
336
- backtrack(digits, index + 1 , s) # 递归,注意index+1,一下层要处理下一个数字
337
- s = s[: - 1 ] # 回溯
338
- backtrack(digits, 0 , s)
339
- return res
338
+ self .answers.clear()
339
+ if not digits: return []
340
+ self .backtracking(digits, 0 , ' ' )
341
+ return self .answers
342
+
343
+ def backtracking ( self , digits : str , index : int , answer : str ) -> None :
344
+ # 回溯函数没有返回值
345
+ # Base Case
346
+ if index == len (digits): # 当遍历穷尽后的下一层时
347
+ self .answers.append(answer)
348
+ return
349
+ # 单层递归逻辑
350
+ letters: str = self .letter_map[digits[index]]
351
+ for letter in letters:
352
+ self .backtracking(digits, index + 1 , answer + letter) # 递归至下一层 + 回溯
340
353
```
341
354
342
355
0 commit comments