diff --git a/problems/0017.电话号码的字母组合.md b/problems/0017.电话号码的字母组合.md index 4f39f60a..df038806 100644 --- a/problems/0017.电话号码的字母组合.md +++ b/problems/0017.电话号码的字母组合.md @@ -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