Restore IP Addresses

My Submissions

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 解题报告的更多相关文章

  1. 【LeetCode】93. Restore IP Addresses 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 日期 题目地址:https://leetco ...

  2. [leetcode]Restore IP Addresses @ Python

    原题地址:https://oj.leetcode.com/problems/restore-ip-addresses/ 题意: Given a string containing only digit ...

  3. [LeetCode] Restore IP Addresses 复原IP地址

    Given a string containing only digits, restore it by returning all possible valid IP address combina ...

  4. LeetCode——Restore IP Addresses

    Given a string containing only digits, restore it by returning all possible valid IP address combina ...

  5. [LeetCode] Restore IP Addresses 回溯

    Given a string containing only digits, restore it by returning all possible valid IP address combina ...

  6. LeetCode Restore IP Addresses

    DFS class Solution { public: vector<string> restoreIpAddresses(string s) { return insertDot(s, ...

  7. 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 ...

  8. 【LeetCode】468. Validate IP Address 解题报告(Python)

    [LeetCode]468. Validate IP Address 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: h ...

  9. leetcode 93. Restore IP Addresses(DFS, 模拟)

    题目链接 leetcode 93. Restore IP Addresses 题意 给定一段序列,判断可能组成ip数的所有可能集合 思路 可以采用模拟或者DFS的想法,把总的ip数分成四段,每段判断是 ...

随机推荐

  1. 算法导论(CLRS)答案

    算法导论(CLRS)答案 Chapter Section I 1 2 p II 1 2 3 p III 1 2 p IV 1 2 3 4 p V 1 2 3 4 p VI 1 2 3 4 5 p VI ...

  2. 【安卓】给gallery内&quot;控件&quot;挂载事件,滑动后抬起手指时也触发事件(滑动时不应触发)的解决、!

    思路: 1.gallery内控件挂载事件(如:onClickListener)的方法类似listview,可直接在baseAdapter.getView内给控件挂载(详细方法百度). 2.貌似没问题, ...

  3. ajax-原理分析

      createTime--2016年9月19日13:59:11Author:Marydon参考链接 http://blog.csdn.net/csh624366188/article/details ...

  4. eclipse 配置多个tomcat

      eclipse 配置多个tomcat CreateTime--2018年4月23日15:32:28 Author:Marydon windows-->Preferences-->Ser ...

  5. python之函数用法xrange()

    # -*- coding: utf-8 -*- #python 27 #xiaodeng #python之函数用法xrange() #xrange() #说明:返回一个生成器 #xrange做循环的性 ...

  6. HTTP协议是如何通信的

    一.什么是HTTP协议 HTTP协议是HyperText Transfer Protocol的缩写,即超文本传输协议.是由w3c(万维网联盟)制定的一种应用层协议,用来定义浏览器与web服务器之间如何 ...

  7. asp web api json 序列化后 把私有字段信息也返回了解决办法

    serialization returns private properties Are your types marked as [Serializable]? Serializable means ...

  8. linux常见面试题及答案

    1. 在Linux系统中,以文件方式访问设备. 2. Linux内核引导时,从文件/etc/fstab中读取要加载的文件系统. 3. Linux文件系统中每个文件用i字节来标识. 4. 全部磁盘块由四 ...

  9. UltraEdit加入到右键菜单中

    http://www.cppblog.com/prayer/archive/2009/02/20/74429.htmlUltraEdit安装好之后,拷贝到其它机器就可以直接使用而无需注册,但少了一个功 ...

  10. linux系统新建用户ssh远程登陆显示-bash-4.1$解决方法,ssh-bash-4.1

    linux系统新建的用户用ssh远程登陆显示-bash-4.1$,不显示用户名路径 网络上好多解决办法,大多是新建.bash_profile文件然后输入XXXXX....然而并没有什么用没有用.... ...