mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 03:34:02 +08:00
添加 0017.电话号码的字母组合 python3版本
添加 0017.电话号码的字母组合 python3版本
This commit is contained in:
@ -272,7 +272,7 @@ class Solution {
|
||||
String str = numString[digits.charAt(num) - '0'];
|
||||
for (int i = 0; i < str.length(); i++) {
|
||||
temp.append(str.charAt(i));
|
||||
//回溯
|
||||
//c
|
||||
backTracking(digits, numString, num + 1);
|
||||
//剔除末尾的继续尝试
|
||||
temp.deleteCharAt(temp.length() - 1);
|
||||
@ -282,7 +282,25 @@ class Solution {
|
||||
```
|
||||
|
||||
Python:
|
||||
|
||||
```python3
|
||||
class Solution:
|
||||
def letterCombinations(self, digits: str) -> List[str]:
|
||||
self.s = ""
|
||||
res = []
|
||||
letterMap = ["","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"]
|
||||
if len(digits) == 0: return res
|
||||
def backtrack(digits,index):
|
||||
if index == len(digits):
|
||||
return res.append(self.s)
|
||||
digit = int(digits[index]) #将index指向的数字转为int
|
||||
letters = letterMap[digit] #取数字对应的字符集
|
||||
for i in range(len(letters)):
|
||||
self.s += letters[i]
|
||||
backtrack(digits,index + 1) #递归,注意index+1,一下层要处理下一个数字
|
||||
self.s = self.s[:-1] #回溯
|
||||
backtrack(digits,0)
|
||||
return res
|
||||
```
|
||||
|
||||
Go:
|
||||
|
||||
|
Reference in New Issue
Block a user