-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0093.RestoreIPAddresses.go
More file actions
58 lines (49 loc) · 1012 Bytes
/
0093.RestoreIPAddresses.go
File metadata and controls
58 lines (49 loc) · 1012 Bytes
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
package main
import (
"strconv"
"strings"
)
func restoreIpAddresses(s string) []string {
res := []string{}
if len(s) < 4 || len(s) > 16 {
return res
}
start := 0
cur := []string{}
backtrack93(s, start, cur, &res)
return res
}
func backtrack93(s string, start int, cur []string, res *[]string) {
if len(cur) == 4 && start == len(s) {
*res = append(*res, strings.Join(cur, "."))
return
}
for i := 1; i < 4; i++ {
if start+i > len(s) {
break
}
temp := s[start : start+i]
if isValidIP(temp, i) {
cur = append(cur, temp)
backtrack93(s, start+i, cur, res)
cur = cur[:len(cur)-1]
}
}
}
func isValidIP(ip string, index int) bool {
if len(ip) == 0 {
return false
}
if ip[0] == '0' && len(ip) > 1 {
return false
}
value, err := strconv.Atoi(ip)
if err != nil {
return false
}
if value > 255 && index == 3 {
return false
}
return true
}
// https://leetcode-cn.com/problems/restore-ip-addresses/solution/jian-dan-yi-yu-li-jie-de-hui-su-fa-java-by-caipeng/