LeetCode 93. 复原 IP 地址
题目描述
考察公司:作业帮
考察时间:2025.5.27
思路分析
回溯问题
参考代码
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
func restoreIpAddresses(s string) []string {
var res []string
var path []string
var dfs func(start int)
dfs = func(start int) {
if len(path) == 4 {
if start == len(s) {
res = append(res, strings.Join(path, "."))
}
return
}
for i := 1; i <= 3; i++ {
if start+i > len(s) {
break
}
segment := s[start : start+i]
if len(segment) > 1 && segment[0] == '0' {
continue
}
num, _ := strconv.Atoi(segment)
if num > 255 {
continue
}
path = append(path, segment)
dfs(start + i)
path = path[:len(path)-1]
}
}
dfs(0)
return res
}
1
write your code here
相似题目
CC BY-NC-SA 4.0
许可协议,转载请注明出处!
本博客所有文章除特别声明外,均采用