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数分成四段,每段判断是 ...
随机推荐
- oracle 批量更新表字段
(一) 将数字替换成汉字 第一步,去重查询 使用distinct关键字先对该字段值进行去重查询,看共有几种情况 --查询指定区间内表停诊字段的值 SELECT DISTINCT T.CLOSE_T ...
- idea 设置黑色背景
idea 设置黑色背景 CreateTime--2018年4月26日10:32:38 Author:Marydon 设置-->Appearance-->Theme调为:Darcula- ...
- Linux 文件系统的目录定义
目录名称 应放置文件的内容 /boot 开机所需文件——内核,开机菜单及所需配置文件等 /dev 任何设备与接口都以文件形式存放在此目录 /etc 配置文件 /home 用户主目录 /bin 单用户维 ...
- 用户研究Q&A(1)
近来,不少同事开始认同用户研究的价值,希望通过接触,理解和研究用户来获取提升产品的有效信息.这绝对是件好事,因为我一直抱持的理念是,研究并不是藏在实验室或者握在少部分人手中的稀罕货,更重要是一种理念和 ...
- 使用padding代替高度实现背景图片高度按比例自适应
本篇文章由:http://xinpure.com/use-padding-instead-of-highly-adaptive-background-image-height-proportionat ...
- App上架重磅通知:App Store安全新规17年1月生效
作者:沙铭 来源:公众号 沙铭世界观 ID:mobview 做推广的也许并不了解什么是ATS(App Transport Security),不过这却是一个定时炸弹,引爆点在2016年底,后果就是你不 ...
- 【LeetCode】132. Palindrome Partitioning II
Palindrome Partitioning II Given a string s, partition s such that every substring of the partition ...
- Windows下面安装和配置Solr 4.9(二)
将Solr和Tomcat结合: 1.在D盘下创建目录 D:\Demos\Solr 2.解压solr-4.9.0文件,我这里下载的是这个4.9版本,将example文件夹下的solr文件夹中的所有文件( ...
- Python学习笔记014——迭代工具函数 内置函数zip()
1 描述 zip() 函数用于将可迭代的对象作为参数,将对象中对应的元素打包成一个个元组,然后返回由这些元组组成的列表. 如果各个迭代器的元素个数不一致,则返回列表长度与最短的对象相同,利用 * 号操 ...
- OAF_OAF Framework状态分析(概念)
20150706 Created By BaoXinjian