This commit is contained in:
youngyangyang04
2021-09-28 15:44:04 +08:00
parent a3a7568e41
commit 8c03e8d213
2 changed files with 34 additions and 63 deletions

View File

@ -8,7 +8,7 @@
## 93.复原IP地址 # 93.复原IP地址
[力扣题目链接](https://leetcode-cn.com/problems/restore-ip-addresses/) [力扣题目链接](https://leetcode-cn.com/problems/restore-ip-addresses/)
@ -19,31 +19,31 @@
例如:"0.1.2.201" 和 "192.168.1.1" 是 有效的 IP 地址,但是 "0.011.255.245"、"192.168.1.312" 和 "192.168@1.1" 是 无效的 IP 地址。 例如:"0.1.2.201" 和 "192.168.1.1" 是 有效的 IP 地址,但是 "0.011.255.245"、"192.168.1.312" 和 "192.168@1.1" 是 无效的 IP 地址。
示例 1 示例 1
输入s = "25525511135" * 输入s = "25525511135"
输出:["255.255.11.135","255.255.111.35"] * 输出:["255.255.11.135","255.255.111.35"]
示例 2 示例 2
输入s = "0000" * 输入s = "0000"
输出:["0.0.0.0"] * 输出:["0.0.0.0"]
示例 3 示例 3
输入s = "1111" * 输入s = "1111"
输出:["1.1.1.1"] * 输出:["1.1.1.1"]
示例 4 示例 4
输入s = "010010" * 输入s = "010010"
输出:["0.10.0.10","0.100.1.0"] * 输出:["0.10.0.10","0.100.1.0"]
示例 5 示例 5
输入s = "101023" * 输入s = "101023"
输出:["1.0.10.23","1.0.102.3","10.1.0.23","10.10.2.3","101.0.2.3"] * 输出:["1.0.10.23","1.0.102.3","10.1.0.23","10.10.2.3","101.0.2.3"]
提示: 提示:
0 <= s.length <= 3000 * 0 <= s.length <= 3000
s 仅由数字组成 * s 仅由数字组成
## 思路 # 思路
做这道题目之前,最好先把[131.分割回文串](https://programmercarl.com/0131.分割回文串.html)这个做了。 做这道题目之前,最好先把[131.分割回文串](https://programmercarl.com/0131.分割回文串.html)这个做了。
@ -114,7 +114,7 @@ if (pointNum == 3) { // 逗点数量为3时分隔结束
代码如下: 代码如下:
``` ```CPP
for (int i = startIndex; i < s.size(); i++) { for (int i = startIndex; i < s.size(); i++) {
if (isValid(s, startIndex, i)) { // 判断 [startIndex,i] 这个区间的子串是否合法 if (isValid(s, startIndex, i)) { // 判断 [startIndex,i] 这个区间的子串是否合法
s.insert(s.begin() + i + 1 , '.'); // 在i的后面插入一个逗点 s.insert(s.begin() + i + 1 , '.'); // 在i的后面插入一个逗点
@ -138,7 +138,7 @@ for (int i = startIndex; i < s.size(); i++) {
代码如下: 代码如下:
``` ```CPP
// 判断字符串s在左闭又闭区间[start, end]所组成的数字是否合法 // 判断字符串s在左闭又闭区间[start, end]所组成的数字是否合法
bool isValid(const string& s, int start, int end) { bool isValid(const string& s, int start, int end) {
if (start > end) { if (start > end) {
@ -237,7 +237,7 @@ public:
``` ```
## 总结 # 总结
在[131.分割回文串](https://programmercarl.com/0131.分割回文串.html)中我列举的分割字符串的难点,本题都覆盖了。 在[131.分割回文串](https://programmercarl.com/0131.分割回文串.html)中我列举的分割字符串的难点,本题都覆盖了。
@ -249,9 +249,9 @@ public:
## 其他语言版本 # 其他语言版本
java 版本: ## java
```java ```java
class Solution { class Solution {
@ -308,7 +308,9 @@ class Solution {
} }
``` ```
python版本: ## python
python2:
```python ```python
class Solution: class Solution:
def restoreIpAddresses(self, s: str) -> List[str]: def restoreIpAddresses(self, s: str) -> List[str]:
@ -338,6 +340,8 @@ class Solution:
backtrack(s, 0) backtrack(s, 0)
return res return res
``` ```
python3:
```python ```python
class Solution(object): class Solution(object):
def restoreIpAddresses(self, s): def restoreIpAddresses(self, s):
@ -366,47 +370,8 @@ class Solution(object):
return ans``` return ans```
``` ```
```python3
class Solution:
def __init__(self) -> None:
self.s = ""
self.res = []
def isVaild(self, s: str) -> bool: ## JavaScript
if len(s) > 1 and s[0] == "0":
return False
if 0 <= int(s) <= 255:
return True
return False
def backTrack(self, path: List[str], start: int) -> None:
if start == len(self.s) and len(path) == 4:
self.res.append(".".join(path))
return
for end in range(start + 1, len(self.s) + 1):
# 剪枝
# 保证切割完s没有剩余的字符。
if len(self.s) - end > 3 * (4 - len(path) - 1):
continue
if self.isVaild(self.s[start:end]):
# 在参数处,更新状态,实则创建一个新的变量
# 不会影响当前的状态当前的path变量没有改变
# 因此递归完不用path.pop()
self.backTrack(path + [self.s[start:end]], end)
def restoreIpAddresses(self, s: str) -> List[str]:
# prune
if len(s) > 3 * 4:
return []
self.s = s
self.backTrack([], 0)
return self.res
```
JavaScript
```js ```js
/** /**
@ -435,8 +400,10 @@ var restoreIpAddresses = function(s) {
} }
}; };
``` ```
Go
> 回溯(对于前导 0的IP特别注意s[startIndex]=='0'的判断不应该写成s[startIndex]==0因为s截取出来不是数字 ## Go
回溯(对于前导 0的IP特别注意s[startIndex]=='0'的判断不应该写成s[startIndex]==0因为s截取出来不是数字
```go ```go
func restoreIpAddresses(s string) []string { func restoreIpAddresses(s string) []string {
@ -476,7 +443,7 @@ func isNormalIp(s string,startIndex,end int)bool{
``` ```
C: ## C
```c ```c
//记录结果 //记录结果
char** result; char** result;

View File

@ -9,3 +9,7 @@
git add之前要git diff 查看一下,本次提交所修改的代码是不是 自己修改的,是否 误删,或者误加的文件。 git add之前要git diff 查看一下,本次提交所修改的代码是不是 自己修改的,是否 误删,或者误加的文件。
提交代码不要使用git push -f 这种命令,要足够了解 -f 意味着什么。 提交代码不要使用git push -f 这种命令,要足够了解 -f 意味着什么。
不用非要写出牛逼的代码才能提交PR只要发现 文章中有任何问题或者错别字都欢迎提交PR成为contributor。
![](https://code-thinking-1253855093.file.myqcloud.com/pics/20210927113149.png)