From 63183226bf48024e7669abd431ac746f6221321f Mon Sep 17 00:00:00 2001 From: Zeeland Date: Thu, 19 Jan 2023 23:56:57 +0800 Subject: [PATCH] =?UTF-8?q?Update=20=E6=B7=BB=E5=8A=A0=E7=94=B5=E8=AF=9D?= =?UTF-8?q?=E5=8F=B7=E7=A0=81=E5=AD=97=E6=AF=8D=E7=BB=84=E5=90=88Python?= =?UTF-8?q?=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0017.电话号码的字母组合.md | 22 ++++++++++++++++++++ 1 file changed, 22 insertions(+) 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