From 5bd02484387de4c317cfeb114cf1de6b04464aca Mon Sep 17 00:00:00 2001 From: ZerenZhang2022 <118794589+ZerenZhang2022@users.noreply.github.com> Date: Mon, 9 Jan 2023 18:10:32 -0500 Subject: [PATCH] =?UTF-8?q?Update=200093.=E5=A4=8D=E5=8E=9FIP=E5=9C=B0?= =?UTF-8?q?=E5=9D=80.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 加入了python3,更类似于LeetCode131题代码的写法,看起来更简洁些 --- problems/0093.复原IP地址.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/problems/0093.复原IP地址.md b/problems/0093.复原IP地址.md index 97178cd5..4e32af99 100644 --- a/problems/0093.复原IP地址.md +++ b/problems/0093.复原IP地址.md @@ -424,6 +424,30 @@ class Solution: return True ``` +python3; 简单拼接版本(类似Leetcode131写法): +```python +class Solution: + def restoreIpAddresses(self, s: str) -> List[str]: + global results, path + results = [] + path = [] + self.backtracking(s,0) + return results + + def backtracking(self,s,index): + global results,path + if index == len(s) and len(path)==4: + results.append('.'.join(path)) # 在连接时需要中间间隔符号的话就在''中间写上对应的间隔符 + return + for i in range(index,len(s)): + if len(path)>3: break # 剪枝 + temp = s[index:i+1] + if (int(temp)<256 and int(temp)>0 and temp[0]!='0') or (temp=='0'): + path.append(temp) + self.backtracking(s,i+1) + path.pop() +``` + ## Go ```go