当前位置:网站首页>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;
}
}
边栏推荐
- The importance of parameter naming, remember a JDBC parameter conflict
- WingIDE 7.2.0 远程调试
- Letter from Silicon Valley: Act fast, Facebook, Quora and other successful "artifacts"!
- Student Information Management System (first time...)
- Flink Yarn Per Job - JobManger 申请 Slot
- 我要抓狂了。。又回到了几天不能A一道题的时候
- 623. 在二叉树中增加一行 : 简单二叉树遍历运用题
- 2022 CCF国际AIOps挑战赛决赛暨AIOps研讨会报名已开启
- TiDB 6.0 Placement Rules In SQL Usage Practice
- PMP每日一练 | 考试不迷路-8.5(包含敏捷+多选)
猜你喜欢

再获殊荣 | 赛宁网安入选2022年度“培育独角兽”企业榜单

没开发人员,接到开发物联网系统的活儿,干不干?

分布式事务解决方案

Http-Sumggling Cache Vulnerability Analysis

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

STM32H743IIT6学习笔记01——CubeMX新建工程文件
字节秋招二面把我干懵了,问我SYN报文什么情况下会被丢弃?

LeetCode brush questions (8)

163_Tricks_Power BI one-click batch creation of custom field parameters
The importance of parameter naming, remember a JDBC parameter conflict
随机推荐
课表小程序使用攻略
版本控制篇 | 龙智邀您共赴GOPS全球运维大会,探索大规模、敏捷、高质量、开放式的软件研发与运营之路
power failure...Trouble trouble trouble!!!
使用Netty编写通用redis客户端(可指定服务器地址与端口号连接任意redis)
hdu 1870 愚人节的礼物 (栈)
Cesium.js 三维土壤地质剖面分割挖掘
Hands-on Deep Learning_GoogLeNet / Inceptionv1v2v3v4
STM32H743IIT6学习笔记02——USART
Zhihu asks: Can China still achieve great national rejuvenation?
灰度值与热成像理解
Gray value and thermal imaging understanding
2022杭电多校联赛第六场 题解
163_技巧_Power BI 一键批量建立自定义字段参数
365天挑战LeetCode1000题——Day 050 在二叉树中增加一行 二叉树
Flink Yarn Per Job - JobManger 申请 Slot
并非富人专属,一文让你对NFT改观
小红的aba子序列(离散化、二分、dp维护区间最短)
Machine Learning - Ensemble Learning
Go编译原理系列9(函数内联)
798. 差分矩阵