-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrestoreipaddresses.go
55 lines (53 loc) · 930 Bytes
/
restoreipaddresses.go
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
package restoreipaddresses
func restoreIPAddresses(s string) []string {
if len(s) <= 3 {
return []string{}
}
result := make([]string, 0, 2)
a, b, c := len(s)-9, 0, 0
if a <= 0 {
a = 1
}
var s1, s2, s3, s4 string
for ; a <= 3; a++ {
for b = a + 1; b <= a+3; b++ {
for c = b + 1; c <= b+3 && c < len(s); c++ {
if c+3 >= len(s) {
if s1 = s[0:a]; invalid(s1) {
continue
}
if s2 = s[a:b]; invalid(s2) {
continue
}
if s3 = s[b:c]; invalid(s3) {
continue
}
if s4 = s[c:]; invalid(s4) {
continue
}
result = append(result, s1+"."+s2+"."+s3+"."+s4)
}
}
}
}
return result
}
func invalid(s string) bool {
if s[0] == '0' {
return len(s) != 1
}
if len(s) == 3 {
if s[0] > '2' {
return true
}
if s[0] == '2' {
if s[1] > '5' {
return true
}
if s[1] == '5' && s[2] > '5' {
return true
}
}
}
return false
}