mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 16:54:50 +08:00
Update 0093.复原IP地址.md
加入了python3,更类似于LeetCode131题代码的写法,看起来更简洁些
This commit is contained in:
@ -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
|
||||
|
Reference in New Issue
Block a user