当前位置:网站首页>Restore IP address [standard backtracking + standard pruning]
Restore IP address [standard backtracking + standard pruning]
2022-06-23 23:51:00 【REN_ Linsen】
Restore IP Address
Preface
Restore IP Address , Standard backtracking search , because IP Address qualification , Standard pruning practice .
One 、 Restore IP Address

Two 、 Backtracking search + Multiple pruning + common bug
package everyday.medium;
import java.util.LinkedList;
import java.util.List;
// Restore IP Address
public class RestoreIpAddresses {
/* target: Give a string s, Split into 4 block , See if it's legal IP Address . legal IP Address :1:0-255;2: Only 4 Additional copies ;3: except 0 No leading is 0 Number of numbers . */
public List<String> restoreIpAddresses(String s) {
List<String> ans = new LinkedList<>();
StringBuilder sb = new StringBuilder();
// enumeration Fill in qualified IP
dfs(s, 0, 0, sb, ans);
// Return to the filled list
return ans;
}
private void dfs(String s, int cur, int n, StringBuilder sb, List<String> ans) {
if (n == 4 && cur == s.length()) {
// Can get here , All meet the conditions IP Address .
sb.deleteCharAt(sb.length() - 1);
ans.add(new String(sb));
// bug2: hold . Deleted , Add it without adding it , During backtracking, there will be deletion of too many items .
sb.append('.');
return;
}
// If you haven't run out of numbers , And it has been divided into four parts , prune ; If not 4 Share , But the numbers are useless , prune .
if (n == 4 || cur == s.length()) return;
// If s.length() - (4 - n) * 4 > 0 when , It is impossible to form IP, prune .
// bug1:s.length() Never change , So there will be >0 advance return The situation of , Should be s.length() - cur - 1
if (s.length() - cur - 1 - (4 - n) * 4 > 0) return;
// Standard backtracking
for (int i = 0; i < 3 && i + cur < s.length(); i++) {
// IP The length is less than 3 position , prune .
// Leading to 0, That can only be 0.
if (i == 0 && s.charAt(cur) == '0') {
sb.append("0.");
dfs(s, cur + 1, n + 1, sb, ans);
sb.delete(sb.length() - 2, sb.length());
break;
}
// Leading non 0
String str = s.substring(cur, cur + i + 1);
int len = str.length();
int val = Integer.parseInt(str);
if (val <= 255) {
sb.append(val).append('.');
dfs(s, cur + i + 1, n + 1, sb, ans);
sb.delete(sb.length() - len - 1, sb.length());
}
}
}
public static void main(String[] args) {
new RestoreIpAddresses().restoreIpAddresses("25525511135");
}
}
summary
1) Backtracking search + Standard pruning + Common backtracking bug
reference
边栏推荐
- What are the good solutions for industrial control safety of production line
- 6月25日PMP考试敏捷怎么考?替你分忧解难
- How to take the PMP Exam agile on June 25? Share your troubles
- 老龄化下背景下,综合能效管理平台为医院保驾护航
- 产线工控安全有什么好的解决方案
- PyQt5_ Qtablewidget paging radio right-click menu control
- Postman返回值中文乱码????
- The input parameter is object, but it was passed as [object object] because it needs to be converted to JSON format
- High imitation Book flag novel flutter edition, learn it
- How to achieve the turning effect of wechat video recording?
猜你喜欢

Tupu software intelligent wind power: operation and maintenance of digital twin 3D wind turbine intelligent equipment
![入参参数为Object,但传递过去却成了[object object] 是因为需要转为JSON格式](/img/8c/b1535e03900d71b075f73f80030064.png)
入参参数为Object,但传递过去却成了[object object] 是因为需要转为JSON格式

How to ensure reliable power supply of Expressway

《德阳市餐饮服务业油烟污染防治管理办法(征求意见稿)》之创新油烟监管

Recommend 4 flutter heavy open source projects

How to achieve energy-saving and reasonable lighting control in order to achieve the "double carbon" goal

Acrel-3000WEB电能管理系统在都巴高速的应用

This high imitation millet mall project is amazing

AUTOCAD——总结CAD画圆角的三种方式

6 大完整开源项目,一次学个够
随机推荐
Why do MySQL indexes use b+ trees at the bottom? After reading this article, you can easily handle the interview.
PyQt5_ Qtablewidget paging radio right-click menu control
E: Unable to acquire lock /var/lib/dpkg/lock
【HackTheBox】Fawn
AUTOCAD——总结CAD画圆角的三种方式
1004. number of maximum consecutive 1 III ●●
Three cool and coquettish bottom navigation
ORB_ Slam3 environment setup and demo demonstration
Simple understanding of responsive programming
2.摄像机标定
Docker Deployment redis
AutoCAD -- summarize three methods of drawing rounded corners in CAD
List<? extends T>和List<?super T>区别
Web site SSL certificate
Task queue of laravel
A cartoon reading app highly imitating Tencent comics
List中subList的add造成的死循环
1.< tag-动态规划和路径组合问题>lt.62. 不同路径 + lt.63. 不同路径 II
Improvement of DC power distribution with open hall current sensor
Recommend 4 flutter heavy open source projects