Skip to content

Commit e09fade

Browse files
committed
add LeetCode 93. 复原IP地址
1 parent d5f0ebd commit e09fade

File tree

1 file changed

+120
-0
lines changed

1 file changed

+120
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
![](https://imgconvert.csdnimg.cn/aHR0cHM6Ly9jZG4uanNkZWxpdnIubmV0L2doL2Nob2NvbGF0ZTE5OTkvY2RuL2ltZy8yMDIwMDgyODE0NTUyMS5qcGc?x-oss-process=image/format,png)
2+
>仰望星空的人,不应该被嘲笑
3+
4+
## 题目描述
5+
给定一个只包含数字的字符串,复原它并返回所有可能的 IP 地址格式。
6+
7+
有效的 IP 地址 正好由四个整数(每个整数位于 0 到 255 之间组成,且不能含有前导 0),整数之间用 '.' 分隔。
8+
9+
例如:`"0.1.2.201" 和 "192.168.1.1"` 是 有效的 IP 地址,但是 `"0.011.255.245"、"192.168.1.312" 和 "[email protected]" ` 是 无效的 IP 地址。
10+
11+
12+
13+
示例 1:
14+
15+
```javascript
16+
输入:s = "25525511135"
17+
输出:["255.255.11.135","255.255.111.35"]
18+
```
19+
20+
示例 2:
21+
22+
```javascript
23+
输入:s = "0000"
24+
输出:["0.0.0.0"]
25+
```
26+
27+
示例 3:
28+
29+
```javascript
30+
输入:s = "1111"
31+
输出:["1.1.1.1"]
32+
```
33+
34+
示例 4:
35+
36+
```javascript
37+
输入:s = "010010"
38+
输出:["0.10.0.10","0.100.1.0"]
39+
```
40+
41+
示例 5:
42+
43+
```javascript
44+
输入:s = "101023"
45+
输出:["1.0.10.23","1.0.102.3","10.1.0.23","10.10.2.3","101.0.2.3"]
46+
47+
```
48+
49+
提示:
50+
51+
```javascript
52+
0 <= s.length <= 3000
53+
s 仅由数字组成
54+
```
55+
56+
来源:力扣(LeetCode)
57+
链接:https://leetcode-cn.com/problems/restore-ip-addresses
58+
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
59+
60+
61+
62+
63+
## 解题思路
64+
直接看图解,显然要用回溯来做,我的做法是对于当前位置,我们可以有三种选择,选一个,选两个,还有选三个。此时就需要判断一下是不是会出现选出边界的情况。
65+
66+
然后对于我们选择的数字,要判断是否出现前导 0 ,同时也要看一下如果是三位数字的话,是不是会超过 255 。题目不能重复选择,于是用组合思想,免去 `vis` 数组。
67+
![](https://img-blog.csdnimg.cn/20200924145613870.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MjQyOTcxOA==,size_16,color_FFFFFF,t_70#pic_center)
68+
借助大佬 <a href="https://leetcode-cn.com/problems/restore-ip-addresses/solution/shou-hua-tu-jie-huan-yuan-dfs-hui-su-de-xi-jie-by-/">xiao_ben_zhu</a> 图解
69+
70+
```javascript
71+
var restoreIpAddresses = function (s) {
72+
let res = [];
73+
let dfs = (cur, start) => {
74+
if (cur.length == 4 && start>=s.length) {
75+
res.push(cur.join('.'));
76+
return;
77+
}
78+
if(cur.length == 4 && start != s.length) return;
79+
for(let k=1;k<=3;k++){
80+
// 如果取的范围超过了字符串长度,直接剪掉
81+
if(start+k-1>=s.length) return;
82+
// 切割字符串
83+
let str = s.substring(start,start+k);
84+
if(str.length>=2 && str[0] == 0) return;
85+
if(str.length>=3 && +str > 255) return;
86+
cur.push(str);
87+
dfs(cur.slice(),start+k);
88+
// 回溯
89+
cur.pop();
90+
}
91+
}
92+
dfs([], 0);
93+
return res;
94+
};
95+
```
96+
97+
98+
99+
## 最后
100+
文章产出不易,还望各位小伙伴们支持一波!
101+
102+
往期精选:
103+
104+
<a href="https://github.com/Chocolate1999/Front-end-learning-to-organize-notes">小狮子前端の笔记仓库</a>
105+
106+
<a href="https://github.com/Chocolate1999/leetcode-javascript">leetcode-javascript:LeetCode 力扣的 JavaScript 解题仓库,前端刷题路线(思维导图)</a>
107+
108+
小伙伴们可以在Issues中提交自己的解题代码,🤝 欢迎Contributing,可打卡刷题,Give a ⭐️ if this project helped you!
109+
110+
111+
<a href="https://yangchaoyi.vip/">访问超逸の博客</a>,方便小伙伴阅读玩耍~
112+
113+
![](https://img-blog.csdnimg.cn/2020090211491121.png#pic_center)
114+
115+
```javascript
116+
学如逆水行舟,不进则退
117+
```
118+
119+
120+
a

0 commit comments

Comments
 (0)