当前位置:网站首页>5.<tag-回溯和切割问题>lt.93.复原IP地址
5.<tag-回溯和切割问题>lt.93.复原IP地址
2022-06-09 11:15:00 【菜菜的大数据开发之路】
lt.93. 复原 IP 地址
[案例需求]

[思路分析]
- startIndex一定是需要的, 因为不能重复分割, 利用startIndex 对下一层递归分割的起始位置进行去重。
- pointNum 变量, 记录添加逗点的数量。


[代码实现]
class Solution {
List<String> result = new ArrayList<>();
public List<String> restoreIpAddresses(String s) {
if (s.length() > 12) return result; // 算是剪枝了
backTrack(s, 0, 0);
return result;
}
// startIndex: 搜索的起始位置, pointNum:添加逗点的数量
private void backTrack(String s, int startIndex, int pointNum) {
if (pointNum == 3) {
// 逗点数量为3时,分隔结束
// 判断第四段⼦字符串是否合法,如果合法就放进result中
if (isValid(s,startIndex,s.length()-1)) {
result.add(s);
}
return;
}
for (int i = startIndex; i < s.length(); i++) {
if (isValid(s, startIndex, i)) {
s = s.substring(0, i + 1) + "." + s.substring(i + 1); //在str的后⾯插⼊⼀个逗点
pointNum++;
backTrack(s, i + 2, pointNum);// 插⼊逗点之后下⼀个⼦串的起始位置为i+2
pointNum--;// 回溯
s = s.substring(0, i + 1) + s.substring(i + 2);// 回溯删掉逗点
} else {
break;
}
}
}
// 判断字符串s在左闭⼜闭区间[start, end]所组成的数字是否合法
private Boolean isValid(String s, int start, int end) {
if (start > end) {
return false;
}
if (s.charAt(start) == '0' && start != end) {
// 0开头的数字不合法
return false;
}
int num = 0;
for (int i = start; i <= end; i++) {
if (s.charAt(i) > '9' || s.charAt(i) < '0') {
// 遇到⾮数字字符不合法
return false;
}
num = num * 10 + (s.charAt(i) - '0');
if (num > 255) {
// 如果⼤于255了不合法
return false;
}
}
return true;
}
}
//方法二:比上面的方法时间复杂度低,更好地剪枝,优化时间复杂度
class Solution {
List<String> result = new ArrayList<String>();
StringBuilder stringBuilder = new StringBuilder();
public List<String> restoreIpAddresses(String s) {
restoreIpAddressesHandler(s, 0, 0);
return result;
}
// number表示stringbuilder中ip段的数量
public void restoreIpAddressesHandler(String s, int start, int number) {
// 如果start等于s的长度并且ip段的数量是4,则加入结果集,并返回
if (start == s.length() && number == 4) {
result.add(stringBuilder.toString());
return;
}
// 如果start等于s的长度但是ip段的数量不为4,或者ip段的数量为4但是start小于s的长度,则直接返回
if (start == s.length() || number == 4) {
return;
}
// 剪枝:ip段的长度最大是3,并且ip段处于[0,255]
for (int i = start; i < s.length() && i - start < 3 && Integer.parseInt(s.substring(start, i + 1)) >= 0
&& Integer.parseInt(s.substring(start, i + 1)) <= 255; i++) {
// 如果ip段的长度大于1,并且第一位为0的话,continue
if (i + 1 - start > 1 && s.charAt(start) - '0' == 0) {
continue;
}
stringBuilder.append(s.substring(start, i + 1));
// 当stringBuilder里的网段数量小于3时,才会加点;如果等于3,说明已经有3段了,最后一段不需要再加点
if (number < 3) {
stringBuilder.append(".");
}
number++;
restoreIpAddressesHandler(s, i + 1, number);
number--;
// 删除当前stringBuilder最后一个网段,注意考虑点的数量的问题
stringBuilder.delete(start + number, i + number + 2);
}
}
}
边栏推荐
- xxl-job 使用初体验
- 流畅看1080p、2k、4k视频需要多大带宽?
- Conversion function between string and numeric value in C language
- 商用密码应用安全性评估
- 03 | definition of China Taiwan: what are we talking about when we talk about China Taiwan?
- 08 | 中台落地第三步:中台的规划与设计(Design)
- 浅谈Event-Loop
- U8g2 graphics library and STM32 migration (I2C, software and hardware)
- 2021年下半年系统集成项目管理工程师综合知识真题及答案解析
- [buuctf.reverse] 108_ [GKCTF 2021]Crash
猜你喜欢

消防工程师与消防员有什么区别?

【SignalR全套系列】之在.Net Core 中实现SignalR实时通信

Win10 20h2 was officially released, and the list of old and new features was compared

还有新产品?丰田GR系列或将扩充产品线

7. remove elements

04 | everything must be done in advance: four issues that must be clearly considered before the construction of China Taiwan Relations
![[patch analysis] cve-2016-8610: patch analysis of](/img/cb/77578a38c2c907c6ee83ac0f70df88.png)
[patch analysis] cve-2016-8610: patch analysis of "SSL death alert" vulnerability leading to denial of service

win10你的组织已关闭自动更新问题怎么解决?

Use of component El scrollbar
![[buuctf.reverse] 109_ [FlareOn6]FlareBear,110_ [INSHack2018]Tricky-Part1](/img/94/324c1bf9e0bd27cdf391d700ec51ec.png)
[buuctf.reverse] 109_ [FlareOn6]FlareBear,110_ [INSHack2018]Tricky-Part1
随机推荐
Answers to six basic questions of security assessment for commercial password applications
[buuctf.reverse] 108_ [GKCTF 2021]Crash
dotnet core 也能协调分布式事务啦!
06 | 中台落地第一步:企业战略分解及现状调研(Discovery)
Go zero micro Service Practice Series (II. Service splitting)
About CSP and its implementation
[buuctf.reverse] 105_ [FlareOn6]Memecat Battlestation
About XSS attack and its defense
On event loop
How to solve the existing 1px problem?
c语言字符串和数值之间的转换函数
go-zero 微服务实战系列(二、服务拆分)
R语言caTools包进行数据划分、randomForest包构建随机森林模型、并查看模型输出的基本信息(树的个数、OOB包外估计误差、混淆矩阵、每个类别误差等)
ConfigurationManager姿势快闪
05 | D4模型:中台规划建设方法论概述
VMware vSphere 6.0 installation and deployment
2021年下半年系统集成项目管理工程师案例分析真题及答案解析
如何解决存在的1px问题?
Iphone5s display disabled solution
【管理知多少】中文“其他”、英文“OTHER”、日文“その他”.......