mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 02:53:31 +08:00
Update 0093.复原IP地址.md
This commit is contained in:
@ -360,104 +360,82 @@ class Solution {
|
|||||||
|
|
||||||
## python
|
## python
|
||||||
|
|
||||||
python2:
|
回溯(版本一)
|
||||||
```python
|
|
||||||
class Solution(object):
|
|
||||||
def restoreIpAddresses(self, s):
|
|
||||||
"""
|
|
||||||
:type s: str
|
|
||||||
:rtype: List[str]
|
|
||||||
"""
|
|
||||||
ans = []
|
|
||||||
path = []
|
|
||||||
def backtrack(path, startIndex):
|
|
||||||
if len(s) > 12: return []
|
|
||||||
if len(path) == 4:
|
|
||||||
if startIndex == len(s):
|
|
||||||
ans.append(".".join(path[:]))
|
|
||||||
return
|
|
||||||
for i in range(startIndex+1, min(startIndex+4, len(s)+1)): # 剪枝
|
|
||||||
string = s[startIndex:i]
|
|
||||||
if not 0 <= int(string) <= 255:
|
|
||||||
continue
|
|
||||||
if not string == "0" and not string.lstrip('0') == string:
|
|
||||||
continue
|
|
||||||
path.append(string)
|
|
||||||
backtrack(path, i)
|
|
||||||
path.pop()
|
|
||||||
|
|
||||||
backtrack([], 0)
|
|
||||||
return ans
|
|
||||||
```
|
|
||||||
|
|
||||||
python3:
|
|
||||||
```python
|
```python
|
||||||
class Solution:
|
class Solution:
|
||||||
def __init__(self):
|
|
||||||
self.result = []
|
|
||||||
|
|
||||||
def restoreIpAddresses(self, s: str) -> List[str]:
|
def restoreIpAddresses(self, s: str) -> List[str]:
|
||||||
'''
|
result = []
|
||||||
本质切割问题使用回溯搜索法,本题只能切割三次,所以纵向递归总共四层
|
self.backtracking(s, 0, 0, "", result)
|
||||||
因为不能重复分割,所以需要start_index来记录下一层递归分割的起始位置
|
return result
|
||||||
添加变量point_num来记录逗号的数量[0,3]
|
|
||||||
'''
|
|
||||||
self.result.clear()
|
|
||||||
if len(s) > 12: return []
|
|
||||||
self.backtracking(s, 0, 0)
|
|
||||||
return self.result
|
|
||||||
|
|
||||||
def backtracking(self, s: str, start_index: int, point_num: int) -> None:
|
def backtracking(self, s, start_index, point_num, current, result):
|
||||||
# Base Case
|
if point_num == 3: # 逗点数量为3时,分隔结束
|
||||||
if point_num == 3:
|
if self.is_valid(s, start_index, len(s) - 1): # 判断第四段子字符串是否合法
|
||||||
if self.is_valid(s, start_index, len(s)-1):
|
current += s[start_index:] # 添加最后一段子字符串
|
||||||
self.result.append(s[:])
|
result.append(current)
|
||||||
return
|
return
|
||||||
# 单层递归逻辑
|
|
||||||
for i in range(start_index, len(s)):
|
for i in range(start_index, len(s)):
|
||||||
# [start_index, i]就是被截取的子串
|
if self.is_valid(s, start_index, i): # 判断 [start_index, i] 这个区间的子串是否合法
|
||||||
if self.is_valid(s, start_index, i):
|
sub = s[start_index:i + 1]
|
||||||
s = s[:i+1] + '.' + s[i+1:]
|
self.backtracking(s, i + 1, point_num + 1, current + sub + '.', result)
|
||||||
self.backtracking(s, i+2, point_num+1) # 在填入.后,下一子串起始后移2位
|
|
||||||
s = s[:i+1] + s[i+2:] # 回溯
|
|
||||||
else:
|
else:
|
||||||
# 若当前被截取的子串大于255或者大于三位数,直接结束本层循环
|
|
||||||
break
|
break
|
||||||
|
|
||||||
def is_valid(self, s: str, start: int, end: int) -> bool:
|
def is_valid(self, s, start, end):
|
||||||
if start > end: return False
|
if start > end:
|
||||||
# 若数字是0开头,不合法
|
|
||||||
if s[start] == '0' and start != end:
|
|
||||||
return False
|
return False
|
||||||
if not 0 <= int(s[start:end+1]) <= 255:
|
if s[start] == '0' and start != end: # 0开头的数字不合法
|
||||||
|
return False
|
||||||
|
num = 0
|
||||||
|
for i in range(start, end + 1):
|
||||||
|
if not s[i].isdigit(): # 遇到非数字字符不合法
|
||||||
|
return False
|
||||||
|
num = num * 10 + int(s[i])
|
||||||
|
if num > 255: # 如果大于255了不合法
|
||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
```
|
|
||||||
|
|
||||||
python3; 简单拼接版本(类似Leetcode131写法):
|
```
|
||||||
|
回溯(版本二)
|
||||||
|
|
||||||
```python
|
```python
|
||||||
class Solution:
|
class Solution:
|
||||||
def restoreIpAddresses(self, s: str) -> List[str]:
|
def restoreIpAddresses(self, s: str) -> List[str]:
|
||||||
global results, path
|
|
||||||
results = []
|
results = []
|
||||||
path = []
|
self.backtracking(s, 0, [], results)
|
||||||
self.backtracking(s,0)
|
|
||||||
return results
|
return results
|
||||||
|
|
||||||
def backtracking(self,s,index):
|
def backtracking(self, s, index, path, results):
|
||||||
global results,path
|
if index == len(s) and len(path) == 4:
|
||||||
if index == len(s) and len(path)==4:
|
results.append('.'.join(path))
|
||||||
results.append('.'.join(path)) # 在连接时需要中间间隔符号的话就在''中间写上对应的间隔符
|
|
||||||
return
|
return
|
||||||
for i in range(index,len(s)):
|
|
||||||
if len(path)>3: break # 剪枝
|
if len(path) > 4: # 剪枝
|
||||||
temp = s[index:i+1]
|
return
|
||||||
if (int(temp)<256 and int(temp)>0 and temp[0]!='0') or (temp=='0'):
|
|
||||||
path.append(temp)
|
for i in range(index, min(index + 3, len(s))):
|
||||||
self.backtracking(s,i+1)
|
if self.is_valid(s, index, i):
|
||||||
|
sub = s[index:i+1]
|
||||||
|
path.append(sub)
|
||||||
|
self.backtracking(s, i+1, path, results)
|
||||||
path.pop()
|
path.pop()
|
||||||
|
|
||||||
|
def is_valid(self, s, start, end):
|
||||||
|
if start > end:
|
||||||
|
return False
|
||||||
|
if s[start] == '0' and start != end: # 0开头的数字不合法
|
||||||
|
return False
|
||||||
|
num = int(s[start:end+1])
|
||||||
|
return 0 <= num <= 255
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
## Go
|
## Go
|
||||||
|
|
||||||
```go
|
```go
|
||||||
|
Reference in New Issue
Block a user