LeetCode: Restore IP Addresses 解题报告
Restore IP Addresses
Question
Solution
Given a string containing only digits, restore it by returning all possible valid IP address combinations.
For example:
Given "25525511135",
return ["255.255.11.135", "255.255.111.35"]. (Order does not matter)
SOLUTION 1:
如果有跟过主页君的讲解,就会知道,这又是一道相当经典的DFS模板题目。 我们照样套用http://www.ninechapter.com/的递归
模板,退出条件是:path.size == 4.
在模板中,我们加入一些判断条件来中断循环:例如说判断pre的字符串转化后的数字是不是在255以内。
另外,我们要排除055这种数字,所以加入这一行判断:
if (s.charAt(index) == '0' && i > index) {
break;
}
虽然是很简单的递归题,但主页君是真心用心写了的。而且这是相当经典的递归模板。同学们一定要记住这种模板解法哦!
public List<String> restoreIpAddresses(String s) {
if (s == null) {
return null;
}
ArrayList<String> ret = new ArrayList<String>();
ArrayList<String> path = new ArrayList<String>();
dfs(s, 0, path, ret);
return ret;
}
public void dfs(String s, int index, ArrayList<String> path, ArrayList<String> ret) {
if (path.size() == 4) {
if (index == s.length()) {
StringBuilder sb = new StringBuilder();
for (String str: path) {
sb.append(str);
sb.append('.');
}
sb.deleteCharAt(sb.length() - 1);
ret.add(sb.toString());
}
return;
}
int len = s.length();
for (int i = index; i < index + 3 && i < len; i++) {
if (s.charAt(index) == '0' && i > index) {
break;
}
String pre = s.substring(index, i + 1);
int num = Integer.parseInt(pre);
if (num > 255) {
continue;
}
path.add(pre);
dfs(s, i + 1, path, ret);
path.remove(path.size() - 1);
}
}
2015.1.1 redo:
容易出错的点:1. i的索引,注意不要越界。2. 记得把sb添加到ret中。
public class Solution {
public List<String> restoreIpAddresses(String s) {
List<String> ret = new ArrayList<String>();
// Bug 1: not length, but length().
if (s == null || s.length() < 4 || s.length() > 12) {
return ret;
}
dfs(s, new ArrayList<String>(), ret, 0);
return ret;
}
public void dfs(String s, List<String> path, List<String> ret, int index) {
// THE BASE CASE:
int len = s.length();
if (path.size() == 4) {
// Create a solution.
if (index == len) {
StringBuilder sb = new StringBuilder();
for (String str: path) {
sb.append(str);
sb.append(".");
}
sb.deleteCharAt(sb.length() - 1);
// bug 3: forget this statement.
ret.add(sb.toString());
}
return;
}
// 2/ 25 / 255
// bug 2: i should < len.
for (int i = index; i < index + 3 && i < len; i++) {
String sub = s.substring(index, i + 1);
if (s.charAt(index) == '0' && i != index) {
// only allow 0, not 02, 022
break;
}
if (!isValid(sub)) {
continue;
}
path.add(sub);
dfs(s, path, ret, i + 1);
path.remove(path.size() - 1);
}
}
public boolean isValid(String s) {
int num = Integer.parseInt(s);
return num >= 0 && num <= 255;
}
}
GITHUB:
https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/string/RestoreIpAddresses.java
LeetCode: Restore IP Addresses 解题报告的更多相关文章
- 【LeetCode】93. Restore IP Addresses 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 日期 题目地址:https://leetco ...
- [leetcode]Restore IP Addresses @ Python
原题地址:https://oj.leetcode.com/problems/restore-ip-addresses/ 题意: Given a string containing only digit ...
- [LeetCode] Restore IP Addresses 复原IP地址
Given a string containing only digits, restore it by returning all possible valid IP address combina ...
- LeetCode——Restore IP Addresses
Given a string containing only digits, restore it by returning all possible valid IP address combina ...
- [LeetCode] Restore IP Addresses 回溯
Given a string containing only digits, restore it by returning all possible valid IP address combina ...
- LeetCode Restore IP Addresses
DFS class Solution { public: vector<string> restoreIpAddresses(string s) { return insertDot(s, ...
- LeetCode解题报告—— Reverse Linked List II & Restore IP Addresses & Unique Binary Search Trees II
1. Reverse Linked List II Reverse a linked list from position m to n. Do it in-place and in one-pass ...
- 【LeetCode】468. Validate IP Address 解题报告(Python)
[LeetCode]468. Validate IP Address 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: h ...
- leetcode 93. Restore IP Addresses(DFS, 模拟)
题目链接 leetcode 93. Restore IP Addresses 题意 给定一段序列,判断可能组成ip数的所有可能集合 思路 可以采用模拟或者DFS的想法,把总的ip数分成四段,每段判断是 ...
随机推荐
- MariaDB初始化和启动故障
初始化故障排查 1. so依赖缺失 比如报这样的错误: ./bin/mysqld: error while loading shared libraries: libnuma.so.1: cannot ...
- 〖Linux〗打开qtcreater出现错误的解决方法
1. 更换了显卡驱动,发现打开qtcreater时出现了以下的错误: qtcreator: error : cannot open shared object file: No such file o ...
- 【LeetCode】【Python题解】Reverse Integer
Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 click to ...
- 执行存储过程报错——ora-01031:权限不足
1. 执行DDL报错 在oracle存储过程中,默认是可以直接执行DML和DQL的,但是执行CREATE这种的DDL则需要借助EXECUTE IMMEDIATE ···了,如下备份表语句 --抄表表备 ...
- js getAttribute getAttributeNode
getAttribute():返回属性值,是一个文本字符串 getAttributeNode("属性名"):返回属性节点,是一个对象 <p id="bj" ...
- <转>C++ explicit关键字详解
要文转自:http://www.cnblogs.com/ymy124/p/3632634.html 首先, C++中的explicit关键字只能用于修饰只有一个参数的类构造函数, 它的作用是表明该构造 ...
- sudo: Cannot execute /usr/local/bin/zsh: No such file or directory 问题
参考:sudo: Cannot execute /usr/local/bin/zsh: No such file or directory 之前在美化Ubuntu的时候,下了个zsh,但是忘记改配置文 ...
- HDU 2604 Queuing (矩阵乘法)
Queuing Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- IP数据报格式和IP地址路由
一.IP数据报格式 IP数据报格式如下: 注:需要注意的是网络数据包以大端字节序传输,当然头部也得是大端字节序,也就是说: The most significant bit is numbered 0 ...
- jquery 事件注冊 与反复事件处理
<!doctype html> <html lang="us"> <head> <meta charset="utf-8&quo ...