mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 00:43:04 +08:00
Update 添加电话号码字母组合Python方法
This commit is contained in:
@ -354,6 +354,28 @@ class Solution:
|
|||||||
for letter in letters:
|
for letter in letters:
|
||||||
self.backtracking(digits, index + 1, answer + letter) # 递归至下一层 + 回溯
|
self.backtracking(digits, index + 1, answer + letter) # 递归至下一层 + 回溯
|
||||||
```
|
```
|
||||||
|
**使用itertools**
|
||||||
|
```python
|
||||||
|
class Solution:
|
||||||
|
def letterCombinations(self, digits: str) -> List[str]:
|
||||||
|
import itertools
|
||||||
|
if not digits:
|
||||||
|
return list()
|
||||||
|
|
||||||
|
phoneMap = {
|
||||||
|
"2": "abc",
|
||||||
|
"3": "def",
|
||||||
|
"4": "ghi",
|
||||||
|
"5": "jkl",
|
||||||
|
"6": "mno",
|
||||||
|
"7": "pqrs",
|
||||||
|
"8": "tuv",
|
||||||
|
"9": "wxyz",
|
||||||
|
}
|
||||||
|
|
||||||
|
groups = (phoneMap[digit] for digit in digits)
|
||||||
|
return ["".join(combination) for combination in itertools.product(*groups)]
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
## Go
|
## Go
|
||||||
|
Reference in New Issue
Block a user