Update 0093.复原IP地址.md

加入了python3,更类似于LeetCode131题代码的写法,看起来更简洁些
This commit is contained in:
ZerenZhang2022
2023-01-09 18:10:32 -05:00
committed by GitHub
parent 4b9d902427
commit 5bd0248438

View File

@ -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