当前位置:网站首页>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;
}
}
边栏推荐
- 高泽龙出席博鳌全球旅游生态大会 讲元宇宙与未来网络科技
- 796. 子矩阵的和
- 前沿技术数字孪生如何应用在智慧城市上?
- Security Issues and Prevention in Web3
- isn't it?Is there anyone who can't locate the slow query problem of MySQL online?
- Memory problems difficult to locate, it is because you do not use ASAN
- 内存问题难定位,那是因为你没用ASAN
- Flink Yarn Per Job - 启动TM,向RM注册,RM分配solt
- Machine Learning - Logistic Regression
- 2022.08.03_每日一题
猜你喜欢

【HMS core】【FAQ】Health Kit、Ads kit、push Kit典型问题合集5

Flink Yarn Per Job - JobManger 申请 Slot

PHP高级检索功能的实现以及动态拼接SQL

STM32H743IIT6学习笔记01——CubeMX新建工程文件

家用电器行业数智化供应链系统:高效整合供应链,提升家电企业核心竞争力

Memory problems difficult to locate, it is because you do not use ASAN

C language classic examples - find the largest number in a series of numbers

STM32H743IIT6学习笔记02——USART

祝所有码农七夕快乐~

Exploration and practice of transaction link under multi-service mode
随机推荐
isn't it?Is there anyone who can't locate the slow query problem of MySQL online?
swig 语法介绍
有多一只“手”的机器狗出没?就在昇腾AI开发者创享日·南京站
C语言例题-计算常量e的值
LeetCode brush questions (8)
Object中的方法
2022 极术通讯-基于安谋科技 “星辰” STAR-MC1的灵动MM32F2570开发板深度评测
Four, kubeadm single master
【硬件架构的艺术】学习笔记(3)处理多个时钟
60行从零开始自己动手写FutureTask是什么体验?
2022.08.01_每日一题
Wingide 快捷键
【无标题】
安装tldr
Machine Learning - Logistic Regression
Visit GOPS Long Zhi booth, Forrester's latest report: "the Forrester Wave: the fourth quarter of 2021 enterprise service management report
2022 CCF International AIOps Challenge Finals and AIOps Seminar Registration Open
关注微信公众号,自动登陆网站
【HMS core】【FAQ】Health Kit、Ads kit、push Kit典型问题合集5
一张图理解EOS是什么