当前位置:网站首页>LeetCode_ 93_ Restore IP address
LeetCode_ 93_ Restore IP address
2022-07-25 21:44:00 【Fitz1318】
Topic link
Title Description
It works IP Address It's just four integers ( Each integer is located in 0 To 255 Between the composition of , And it can't contain leading 0), Use... Between integers ‘.’ Separate .
- for example :
"0.1.2.201"and"192.168.1.1"yes It works IP Address , however"0.011.255.245"、"192.168.1.312"and"[email protected]"yes Invalid IP Address .
Given a string containing only numbers s , It is used to indicate a IP Address , Return all possible It works IP Address , These addresses can be accessed through s Insert '.' To form . you You can't Reorder or delete s Any number in . You can press whatever Return the answers in order .
Example 1:
Input :s = "25525511135"
Output :["255.255.11.135","255.255.111.35"]
Example 2:
Input :s = "0000"
Output :["0.0.0.0"]
Example 3:
Input :s = "101023"
Output :["1.0.10.23","1.0.102.3","10.1.0.23","10.10.2.3","101.0.2.3"]
Tips :
1 <= s.length <= 20sIt's just numbers
Their thinking
Retrospective recursive trilogy
- Parameters and return values of recursive functions
s: character stringstartIndex: control for looppointNum: Indicates the number of commas , It has been used to record several paragraphsans: Set of qualified results
- Termination condition of recursive function
- When
pointNum == 3It indicates that the string has been divided into 4 paragraph , It's in line with IP The rules of address - Then verify whether the fourth paragraph is legal , If it's legal, add it to the result set
- When
- Single layer logic
[startIndex,i]This interval is the substring intercepted , You need to judge whether this substring is legal , If it is legal, add a match after the string.Indicates that it has been divided- If it is not legal, end this layer cycle
Judge the legitimacy of substring
- Duan Weiyi 0 The number beginning with bit is illegal
- It is illegal to have negative integer characters in the segment
- If it is greater than 255 It is illegal.
- Other situations are legal
AC Code
class Solution {
public List<String> restoreIpAddresses(String s) {
List<String> ans = new ArrayList<>();
backTracing(s, 0, 0, ans);
return ans;
}
private static void backTracing(String s, int startIndex, int pointNum, List<String> ans) {
// Termination conditions
if (pointNum == 3) {
if (isValid(s, startIndex, s.length() - 1)) {
ans.add(s);
}
return;
}
for (int i = startIndex; i < s.length(); i++) {
if (isValid(s, startIndex, i)) {
// stay str Add a point after
s = s.substring(0, i + 1) + "." + s.substring(i + 1);
pointNum++;
backTracing(s, i + 2, pointNum, ans);
pointNum--;
s = s.substring(0, i + 1) + s.substring(i + 2);
} else {
break;
}
}
}
private static boolean isValid(String s, int start, int end) {
if (start > end) {
return false;
}
// Leading 0 The number of is illegal
if (s.charAt(start) == '0' && start != end) {
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) {
return false;
}
}
return true;
}
}
边栏推荐
- 我也是醉了,Eureka 延迟注册还有这个坑!
- Zero basic learning canoe panel (17) -- panel CAPL function
- Protobuf的简单使用
- 919. 完全二叉树插入器 : 简单 BFS 运用题
- Lichuang EDA -- creation of devices 01 resistance (II)
- The role of the resize function is "suggestions collection"
- How to evaluate hardware resources (number of CPUs, memory size) when Oracle migrates from small computers to x86 architecture? Is there a measurement index or company?
- Composition of dog food
- Kali modify the update source (it is not safe to update with this source)
- 【面试:并发篇25:多线程:volatile】可见性
猜你喜欢

Optimization analysis of storage structure and IO performance of openharmony littlefs file system

strcpy()

ONEFLOW V0.8.0 officially released

Face and key point detection: yolo5face practice

ag 搜索工具参数详解

2022 latest examination questions and answers of eight members (standard staff) of Shanghai Architecture

新版Maixhub部署(V831与K210)
![[MAIXPY]kpu: load error:2005, ERR_READ_FILE: read file failed问题解决](/img/0b/da67b5a361a2cdfaf81568d34cf5f7.png)
[MAIXPY]kpu: load error:2005, ERR_READ_FILE: read file failed问题解决

立创EDA——我为什么要学EDA

Per capita Swiss number series, Swiss number 4 generation JS reverse analysis
随机推荐
Autojs learning - realize 3D perspective
Job interviews are always a second kill? After reading the seckill system notes secretly stored by JD T8, I have given my knees
Fastjson deserialization vulnerability utilization analysis collection
Kali modify the update source (it is not safe to update with this source)
Autojs learning - Automatic screenshot of the king
分享|智慧消防应急管理平台解决方案(附PDF)
H5 realize the animation effect of a scratch card
Pyqt5 use pyqtgraph to draw multiple y-value scatter plots
[interview: concurrent Article 23: multithreading: Join] re understanding of join
【面试:并发篇23:多线程:join】join再理解
QT | learn about QT creator by creating a simple project
C#常见的集合
Web3 entrepreneurship has all the elements of explosive growth of innovation
Unity metaverse (II), mixamo & animator hybrid tree and animation fusion
Stm3 (cubeide) lighting experiment
Redis 使用详解
ES6 -- Deconstruction assignment
[interview: concurrent 25: multithreading: volatile] visibility
Ijcai2022 meeting! Microsoft and other tutorials on domain generalization
[MAIXPY]kpu: load error:2005, ERR_READ_FILE: read file failed问题解决