当前位置:网站首页>2022.08.03_每日一题
2022.08.03_每日一题
2022-08-05 11:48:00 【诺.い】
93. 复原 IP 地址
题目描述
有效 IP 地址 正好由四个整数(每个整数位于 0 到 255 之间组成,且不能含有前导 0),整数之间用 '.' 分隔。
- 例如:
"0.1.2.201"和"192.168.1.1"是 有效 IP 地址,但是"0.011.255.245"、"192.168.1.312"和"[email protected]"是 无效 IP 地址。
给定一个只包含数字的字符串 s ,用以表示一个 IP 地址,返回所有可能的有效 IP 地址,这些地址可以通过在 s 中插入 '.' 来形成。你 不能 重新排序或删除 s 中的任何数字。你可以按 任何 顺序返回答案。
示例 1:
输入:s = “25525511135”
输出:[“255.255.11.135”,“255.255.111.35”]
示例 2:
输入:s = “0000”
输出:[“0.0.0.0”]
示例 3:
输入:s = “101023”
输出:[“1.0.10.23”,“1.0.102.3”,“10.1.0.23”,“10.10.2.3”,“101.0.2.3”]
提示:
1 <= s.length <= 20s仅由数字组成
coding
class Solution {
String s;
int len;
// cnt : 记录当前 '.' 的数量
int cnt = 0;
List<String> res;
public List<String> restoreIpAddresses(String s) {
this.s = s;
this.len = s.length();
this.res = new ArrayList<>();
// s 过长或过短
if(len < 4 || len > 12) {
return res;
}
dfs(0, new StringBuilder(len + 3));
return res;
}
// index : 下一个拼接字串的首字母索引
// sb : 记录当前已经符合条件的部分 ip
public void dfs(int index, StringBuilder sb) {
// 已经拥有 3 个 '.', 只需判断最后剩余的字串是否满足条件
// 若满足, 则直接计入结果, 否则直接结束方法
if (cnt == 3) {
if(isOK(index, len)){
sb.append(s.substring(index, len));
res.add(new String(sb));
sb.delete(index + cnt, len + 3);
}
return;
}
for (int i = 1; i < 4; i++) {
if (index + i <= len && isOK(index, index + i)) {
sb.append(s.substring(index, index + i));
sb.append('.');
cnt ++;
dfs(index + i, sb);
// 回溯
sb.delete(index + cnt - 1, index + i + cnt);
cnt --;
} else {
return;
}
}
}
// 判断子字符串是否符合条件
public boolean isOK(int l, int r) {
if(r - l < 1 || (r - l > 1 && (s.charAt(l) == '0' || Integer.parseInt(s.substring(l, r)) > 255))) {
return false;
}
return true;
}
}
边栏推荐
- How to use the timetable applet
- Grid Infrastructure Installation Fails with Error
- Http-Sumggling Cache Vulnerability Analysis
- 课表小程序使用攻略
- 对于聚合物聚乙二醇PEG大家了解多少了?以及在生活中的应用
- 莅临GOPS大会龙智展位,获取Forrester最新报告:《Forrester Wave:2021年第四季度企业服务管理报告》
- hdu2097 nyoj414 sky数 (进制转换)
- Wingide 快捷键
- 互联网行业凛冬之至,BATM的程序员是如何应对中年危机的?
- swig 语法介绍
猜你喜欢

Integration testing of software testing

2022 CCF International AIOps Challenge Finals and AIOps Seminar Registration Open

Go编译原理系列9(函数内联)

Machine Learning - Logistic Regression

Four, kubeadm single master

“小钢炮”气质明显,安全、舒适一个不落

Flink Yarn Per Job - 启动TM,向RM注册,RM分配solt

硅谷来信:快速行动,Facebook、Quora等成功的“神器”!

【着色器实现Flicker“DJ”闪烁效果_Shader效果第十五篇】

冬日里,28℃的爱情
随机推荐
Linux: Remember to install MySQL8 on CentOS7 (blog collection)
互联网行业凛冬之至,BATM的程序员是如何应对中年危机的?
hdu1455 Sticks(搜索+剪枝+剪枝+.....+剪枝)
申请百度地图API Key进行百度地图开发,获取经纬度对应地点
nyoj757 期末考试 (优先队列)
Gray value and thermal imaging understanding
STM32H743IIT6学习笔记03——使用第三方组件FreeRTOS
学习用于视觉跟踪的深度紧凑图像表示
The training set Loss converges, but the test set Loss oscillates violently?
PHP高级检索功能的实现以及动态拼接SQL
797. 差分
支持向量机SVM
[7.29-8.5] Review of wonderful technical blog posts in the writing community
Keras 分割网络自定义评估函数 - mean iou
花的含义
Flink Yarn Per Job - JobManger 申请 Slot
分布式事务解决方案
I'm going crazy.Again A few days can not be A problem
【HMS core】【FAQ】Health Kit、Ads kit、push Kit典型问题合集5
【着色器实现Flicker“DJ”闪烁效果_Shader效果第十五篇】