Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[pull] master from youngyangyang04:master #275

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
pull merged 24 commits into AlgorithmAndLeetCode:master from youngyangyang04:master
May 29, 2023
Merged
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
2f86a5c
Update 0701.二叉搜索树中的插入操作.md
jianghongcheng May 25, 2023
343e077
Update 0216.组合总和III.md
jianghongcheng May 26, 2023
89e9d75
Update 0077.组合.md
jianghongcheng May 26, 2023
4d48c45
Update 0077.组合优化.md
jianghongcheng May 26, 2023
847c60a
Update 0017.电话号码的字母组合.md
jianghongcheng May 26, 2023
2c51420
Update 0039.组合总和.md
jianghongcheng May 26, 2023
20db57f
Update 0040.组合总和II.md
jianghongcheng May 26, 2023
59742e5
Update 0040.组合总和II.md
jianghongcheng May 26, 2023
4586386
Update 0131.分割回文串.md
jianghongcheng May 27, 2023
78445d7
Update 0131.分割回文串.md
jianghongcheng May 27, 2023
51c8976
Update 0093.复原IP地址.md
jianghongcheng May 27, 2023
d45c141
Update 0078.子集.md
jianghongcheng May 27, 2023
5801ae7
Update 0090.子集II.md
jianghongcheng May 27, 2023
eb72f26
Update 0491.递增子序列.md
jianghongcheng May 28, 2023
7e3fc8e
Update 0491.递增子序列.md
jianghongcheng May 28, 2023
a6c95b7
Update 0491.递增子序列.md
jianghongcheng May 28, 2023
b033a08
Update 0046.全排列.md
jianghongcheng May 28, 2023
fd2608a
Update 0047.全排列II.md
jianghongcheng May 28, 2023
19ecde0
Update 回溯算法去重问题的另一种写法.md
jianghongcheng May 28, 2023
498395d
Update 0332.重新安排行程.md
jianghongcheng May 28, 2023
93e0a18
Update 0051.N皇后.md
jianghongcheng May 28, 2023
11581df
Update 0455.分发饼干.md
jianghongcheng May 28, 2023
18a406e
Update 0376.摆动序列.md
jianghongcheng May 28, 2023
996ddbe
Merge pull request #2098 from jianghongcheng/master
youngyangyang04 May 29, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Update 0093.复原IP地址.md
  • Loading branch information
jianghongcheng authored May 27, 2023
commit 51c89762f6c9643f76da1ea7fb516ca204fd8634
134 changes: 56 additions & 78 deletions problems/0093.复原IP地址.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -360,104 +360,82 @@ class Solution {

## 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
class Solution:
def __init__(self):
self.result = []

def restoreIpAddresses(self, s: str) -> List[str]:
'''
本质切割问题使用回溯搜索法,本题只能切割三次,所以纵向递归总共四层
因为不能重复分割,所以需要start_index来记录下一层递归分割的起始位置
添加变量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:
# Base Case
if point_num == 3:
if self.is_valid(s, start_index, len(s)-1):
self.result.append(s[:])
result = []
self.backtracking(s, 0, 0, "", result)
return result

def backtracking(self, s, start_index, point_num, current, result):
if point_num == 3: # 逗点数量为3时,分隔结束
if self.is_valid(s, start_index, len(s) - 1): # 判断第四段子字符串是否合法
current += s[start_index:] # 添加最后一段子字符串
result.append(current)
return
# 单层递归逻辑

for i in range(start_index, len(s)):
# [start_index, i]就是被截取的子串
if self.is_valid(s, start_index, i):
s = s[:i+1] + '.' + s[i+1:]
self.backtracking(s, i+2, point_num+1) # 在填入.后,下一子串起始后移2位
s = s[:i+1] + s[i+2:] # 回溯
if self.is_valid(s, start_index, i): # 判断 [start_index, i] 这个区间的子串是否合法
sub = s[start_index:i + 1]
self.backtracking(s, i + 1, point_num + 1, current + sub + '.', result)
else:
# 若当前被截取的子串大于255或者大于三位数,直接结束本层循环
break

def is_valid(self, s: str, start: int, end: int) -> bool:
if start > end: return False
# 若数字是0开头,不合法
if s[start] == '0' and start != end:

def is_valid(self, s, start, end):
if start > end:
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 True

```
回溯(版本二)

python3; 简单拼接版本(类似Leetcode131写法):
```python
class Solution:
class Solution:
def restoreIpAddresses(self, s: str) -> List[str]:
global results, path
results = []
path = []
self.backtracking(s,0)
self.backtracking(s, 0, [], results)
return results

def backtracking(self,s,index):
global results,path
if index == len(s) and len(path)==4:
results.append('.'.join(path)) # 在连接时需要中间间隔符号的话就在''中间写上对应的间隔符
def backtracking(self, s, index, path, results):
if index == len(s) and len(path) == 4:
results.append('.'.join(path))
return

if len(path) > 4: # 剪枝
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()

for i in range(index, min(index + 3, len(s))):
if self.is_valid(s, index, i):
sub = s[index:i+1]
path.append(sub)
self.backtracking(s, i+1, path, results)
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
Expand Down

AltStyle によって変換されたページ (->オリジナル) /