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. openssl的证书格式转换

    证书转换 PKCS 全称是 Public-Key Cryptography Standards ,是由 RSA 实验室与其它安全系统开发商为促进公钥密码的发展而制订的一系列标准,PKCS 目前共发布过 ...

  2. hdu 4857 逃生 (拓扑排序+保证最小在前面)

    逃生 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submiss ...

  3. HDUOJ---1233还是畅通工程

    还是畅通工程 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  4. HDUOJ--8球胜负

    8球胜负 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submi ...

  5. 【LeetCode】47. Permutations II

    Permutations II Given a collection of numbers that might contain duplicates, return all possible uni ...

  6. Java虚拟机学习 - 内存调优 ( 9 )

    JVM调优主要是针对内存管理方面的调优,包括控制各个代的大小,GC策略.由于GC开始垃圾回收时会挂起应用线程,严重影响了性能,调优的目是为了尽量降低GC所导致的应用线程暂停时间. 减少Full GC次 ...

  7. 图解最小生成树 - 普里姆(Prim)算法

    我们在图的定义中说过,带有权值的图就是网结构.一个连通图的生成树是一个极小的连通子图,它含有图中全部的顶点,但只有足以构成一棵树的n-1条边.所谓的最小成本,就是n个顶点,用n-1条边把一个连通图连接 ...

  8. Bitmap具体解释与Bitmap的内存优化

    感觉这里的排版看着更舒服些 Bitmap具体解释与Bitmap的内存优化 一.Bitmap: Bitmap是Android系统中的图像处理的最重要类之中的一个.用它能够获取图像文件信息,进行图像剪切. ...

  9. SQL 语句递归查询 With AS 查找所有子节点

    create table #EnterPrise (   Department nvarchar(50),--部门名称   ParentDept nvarchar(50),--上级部门   Depar ...

  10. 使用NuGet发布自己的.NET NuGet 包( .NET Standard & Windows)

    发布自己的nuget包 STEP 1:获取API Key 首先,你需要到NuGet上注册一个新的账号,然后在My Account页面,获取一个API Key,或者是自建服务器上生成一个API Key( ...