From ac005060cd3f8184b0d1c47947ee3935b1638067 Mon Sep 17 00:00:00 2001 From: jojoo15 <75017412+jojoo15@users.noreply.github.com> Date: Mon, 24 May 2021 18:52:00 +0200 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200017.=E7=94=B5=E8=AF=9D?= =?UTF-8?q?=E5=8F=B7=E7=A0=81=E7=9A=84=E5=AD=97=E6=AF=8D=E7=BB=84=E5=90=88?= =?UTF-8?q?=20python3=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加 0017.电话号码的字母组合 python3版本 --- problems/0017.电话号码的字母组合.md | 22 ++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/problems/0017.电话号码的字母组合.md b/problems/0017.电话号码的字母组合.md index ce1f63fb..0a69c69c 100644 --- a/problems/0017.电话号码的字母组合.md +++ b/problems/0017.电话号码的字母组合.md @@ -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: