-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution93.java
More file actions
63 lines (56 loc) · 1.92 KB
/
Copy pathSolution93.java
File metadata and controls
63 lines (56 loc) · 1.92 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package medium;
import java.util.LinkedList;
import java.util.List;
public class Solution93 {
public static void main(String[] args) {
Solution93 s = new Solution93();
String[] cases = {"25525511135"};
for (String c : cases) System.out.println(s.restoreIpAddresses(c));
}
public List<String> restoreIpAddresses(String s) {
List<String> result = new LinkedList<>();
char dot = '.';
int len = s.length(), max = Math.min(len - 2, 6);
for (int mid = Math.max(2, len - 6); mid <= max; mid++) {
List<Integer> p = new LinkedList<>(), n = new LinkedList<>();
int temp = Math.min(mid - 1, 3);
for (int i = Math.max(1, mid - 3); i <= temp; i++)
if (isByte(s, 0, i) && isByte(s, i, mid))
p.add(i);
if (p.isEmpty()) continue;
temp = Math.min(len - 1, mid + 3);
for (int i = Math.max(mid + 1, len - 3); i <= temp; i++)
if (isByte(s, mid, i) && isByte(s, i, len))
n.add(i);
if (n.isEmpty()) continue;
for (int a : p)
for (int b : n)
result.add(new StringBuilder(s).insert(b, dot).insert(mid, dot).insert(a, dot).toString());
}
return result;
}
private static boolean isByte(String s, int x, int y) {
switch (y - x) {
case 1:
return true;
case 2:
return s.charAt(x) != '0';
case 3:
break;
default:
return false;
}
switch (s.charAt(x)) {
case '1':
return true;
case '2':
break;
default:
return false;
}
char c = s.charAt(x + 1);
if (c < '5') return true;
if (c > '5') return false;
return s.charAt(x + 2) <= '5';
}
}