mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-06 23:28:29 +08:00
Merge pull request #1870 from Undertone0809/patch-7
Update 添加电话号码字母组合Python新方法
This commit is contained in:
@ -354,6 +354,28 @@ class Solution:
|
||||
for letter in letters:
|
||||
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
|
||||
|
Reference in New Issue
Block a user