-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRestoreIPAddresses.java
More file actions
67 lines (43 loc) · 1.84 KB
/
RestoreIPAddresses.java
File metadata and controls
67 lines (43 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package company;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Stream;
/**
* @Author: Wenhang Chen
* @Description:给定一个只包含数字的字符串,复原它并返回所有可能的 IP 地址格式。
* <p>
* 示例:
* <p>
* 输入: "25525511135"
* 输出: ["255.255.11.135", "255.255.111.35"]
* @Date: Created in 17:24 2/7/2020
* @Modified by:
*/
public class RestoreIPAddresses {
public List<String> restoreIpAddresses(String s) {
List<String> ret = new ArrayList<>();
StringBuilder ip = new StringBuilder();
// 四层for循环解千愁
for (int a = 1; a < 4; ++a)
for (int b = 1; b < 4; ++b)
for (int c = 1; c < 4; ++c)
for (int d = 1; d < 4; ++d) {
if (a + b + c + d == s.length()) {
// 截取一段,转为数字
int n1 = Integer.parseInt(s.substring(0, a));
int n2 = Integer.parseInt(s.substring(a, a + b));
int n3 = Integer.parseInt(s.substring(a + b, a + b + c));
int n4 = Integer.parseInt(s.substring(a + b + c));
if (n1 <= 255 && n2 <= 255 && n3 <= 255 && n4 <= 255) {
ip.append(n1).append('.').append(n2)
.append('.').append(n3).append('.').append(n4);
// 防止出现形如 01 被解析为 1 的情况,以 0 开头的那段必定为 0
if (ip.length() == s.length() + 3) ret.add(ip.toString());
ip.delete(0, ip.length());
}
}
}
return ret;
}
}