From 8c03e8d213cbc62a85ab66f97bf2b09179f69e7a Mon Sep 17 00:00:00 2001 From: youngyangyang04 <826123027@qq.com> Date: Tue, 28 Sep 2021 15:44:04 +0800 Subject: [PATCH] Update --- problems/0093.复原IP地址.md | 93 ++++++++++-------------------- problems/其他/参与本项目.md | 4 ++ 2 files changed, 34 insertions(+), 63 deletions(-) diff --git a/problems/0093.复原IP地址.md b/problems/0093.复原IP地址.md index 52db821a..7639bea2 100644 --- a/problems/0093.复原IP地址.md +++ b/problems/0093.复原IP地址.md @@ -8,7 +8,7 @@ -## 93.复原IP地址 +# 93.复原IP地址 [力扣题目链接](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 地址。 示例 1: -输入:s = "25525511135" -输出:["255.255.11.135","255.255.111.35"] +* 输入:s = "25525511135" +* 输出:["255.255.11.135","255.255.111.35"] 示例 2: -输入:s = "0000" -输出:["0.0.0.0"] +* 输入:s = "0000" +* 输出:["0.0.0.0"] 示例 3: -输入:s = "1111" -输出:["1.1.1.1"] +* 输入:s = "1111" +* 输出:["1.1.1.1"] 示例 4: -输入:s = "010010" -输出:["0.10.0.10","0.100.1.0"] +* 输入:s = "010010" +* 输出:["0.10.0.10","0.100.1.0"] 示例 5: -输入:s = "101023" -输出:["1.0.10.23","1.0.102.3","10.1.0.23","10.10.2.3","101.0.2.3"] +* 输入:s = "101023" +* 输出:["1.0.10.23","1.0.102.3","10.1.0.23","10.10.2.3","101.0.2.3"] 提示: -0 <= s.length <= 3000 -s 仅由数字组成 +* 0 <= s.length <= 3000 +* s 仅由数字组成 -## 思路 +# 思路 做这道题目之前,最好先把[131.分割回文串](https://programmercarl.com/0131.分割回文串.html)这个做了。 @@ -114,7 +114,7 @@ if (pointNum == 3) { // 逗点数量为3时,分隔结束 代码如下: -``` +```CPP for (int i = startIndex; i < s.size(); i++) { if (isValid(s, startIndex, i)) { // 判断 [startIndex,i] 这个区间的子串是否合法 s.insert(s.begin() + i + 1 , '.'); // 在i的后面插入一个逗点 @@ -138,7 +138,7 @@ for (int i = startIndex; i < s.size(); i++) { 代码如下: -``` +```CPP // 判断字符串s在左闭又闭区间[start, end]所组成的数字是否合法 bool isValid(const string& s, int start, int end) { if (start > end) { @@ -237,7 +237,7 @@ public: ``` -## 总结 +# 总结 在[131.分割回文串](https://programmercarl.com/0131.分割回文串.html)中我列举的分割字符串的难点,本题都覆盖了。 @@ -249,9 +249,9 @@ public: -## 其他语言版本 +# 其他语言版本 -java 版本: +## java ```java class Solution { @@ -308,7 +308,9 @@ class Solution { } ``` -python版本: +## python + +python2: ```python class Solution: def restoreIpAddresses(self, s: str) -> List[str]: @@ -338,6 +340,8 @@ class Solution: backtrack(s, 0) return res ``` + +python3: ```python class Solution(object): def restoreIpAddresses(self, s): @@ -366,47 +370,8 @@ class Solution(object): return ans``` ``` -```python3 -class Solution: - def __init__(self) -> None: - self.s = "" - self.res = [] - def isVaild(self, s: str) -> bool: - 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: +## JavaScript ```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 func restoreIpAddresses(s string) []string { @@ -476,7 +443,7 @@ func isNormalIp(s string,startIndex,end int)bool{ ``` -C: +## C ```c //记录结果 char** result; diff --git a/problems/其他/参与本项目.md b/problems/其他/参与本项目.md index cfa75439..76a2c61e 100644 --- a/problems/其他/参与本项目.md +++ b/problems/其他/参与本项目.md @@ -9,3 +9,7 @@ git add之前,要git diff 查看一下,本次提交所修改的代码是不是 自己修改的,是否 误删,或者误加的文件。 提交代码,不要使用git push -f 这种命令,要足够了解 -f 意味着什么。 + + +不用非要写出牛逼的代码才能提交PR,只要发现 文章中有任何问题,或者错别字,都欢迎提交PR,成为contributor。 +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20210927113149.png)