当前位置:网站首页>2022.08.03_Daily Question
2022.08.03_Daily Question
2022-08-05 11:54:00 【No. い】
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 : The initial index of the next concatenated string
// sb : Record the parts that are currently eligible ip
public void dfs(int index, StringBuilder sb) {
// 已经拥有 3 个 '.', Just check whether the last remaining string satisfies the condition
// 若满足, directly included in the result, Otherwise, end the method directly
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;
}
}
}
// Check whether the substring meets the condition
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
- Version Control | Longzhi invites you to go to the GOPS Global Operation and Maintenance Conference to explore the road of large-scale, agile, high-quality and open software development and operation
- 尚硅谷-JVM-内存和垃圾回收篇(P1~P203)
- 2022.08.01_每日一题
- Byte Qiu Zhao confused me on both sides, and asked me under what circumstances would the SYN message be discarded?
- 【无标题】
- C语言经典例题-求一串数中的最大数
- 60行从零开始自己动手写FutureTask是什么体验?
- 2022.08.01_每日一题
- “蘑菇书”是怎样磨出来的?
猜你喜欢

使用Netty编写通用redis客户端(可指定服务器地址与端口号连接任意redis)

D-Desthiobiotin-PEG4-Maleimide主要物理性质特点 !

Gao Zelong attended the Boao Global Tourism Ecology Conference to talk about Metaverse and Future Network Technology

Security Issues and Prevention in Web3

Web3 中的安全问题和防范

2022.08.01_每日一题

Go compilation principle series 9 (function inlining)

Shang Silicon Valley-JVM-Memory and Garbage Collection (P1~P203)

动手学深度学习_GoogLeNet / Inceptionv1v2v3v4

KVM virtualization technology-NUMA technology and application
随机推荐
Go 语言 strings 库常用方法
2022.08.01_每日一题
训练集Loss收敛,但是测试集Loss震荡的厉害?
KVM virtualization technology-NUMA technology and application
WPF开发随笔收录-WriteableBitmap绘制高性能曲线图
解决运行文件消失、C盘空间不断缩小而且找不到文件位置的问题
Five reasons why developers choose Klocwork, a static analysis tool for code quality, for software security
789. 数的范围
【硬件架构的艺术】学习笔记(3)处理多个时钟
knife4j
STM32H743IIT6学习笔记02——USART
C语言例题-打印日历
可视化开发必看:智慧城市四大核心技术
todolist案列——原生js
深度学习(四)分析问题与调参 理论部分
高泽龙出席博鳌全球旅游生态大会 讲元宇宙与未来网络科技
treeselect common function record (with a callback function for clearing options)
动手学深度学习_GoogLeNet / Inceptionv1v2v3v4
LeetCode刷题(8)
使用Netty编写通用redis客户端(可指定服务器地址与端口号连接任意redis)