[抄题]:

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

Example:

Input: "25525511135"
Output: ["255.255.11.135", "255.255.111.35"]

[暴力解法]:

时间分析:

空间分析:

[优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

变量多的时候就一个个看:有单一变量的范围限制,也有多重变量的范围限制。比如新加单词后过长:pos + i > s.length()

[思维问题]:

以为要用backtracing,不知道那样的1到3位怎么加。其实加不了的话就写一般的dfs就行了。

[英文数据结构或算法,为什么不用别的数据结构或算法]:

[一句话思路]:

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. .substring方法是小写
  2. 需要用到dfs的思想:i从1到3中的一个进去,dfs 还是会for 1到3

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

需要用到dfs的思想:i从1到3中的一个进去,dfs 还是会for 1到3

[复杂度]:Time complexity: O(n) Space complexity: O(n)

[算法思想:迭代/递归/分治/贪心]:

[关键模板化代码]:

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

[代码风格] :

[是否头一次写此类driver funcion的代码] :

[潜台词] :

class Solution {
public List<String> restoreIpAddresses(String s) {
//initialization
List<String> result = new ArrayList<String>(); //corner case
if (s == null || s.length() > 12) return result; //generateIpAddresses
generateIpAddresses(s, 0, 0, "", result); //return
return result;
} public void generateIpAddresses(String s, int sec, int pos, String curIP, List<String> result) {
//exit case
if (sec > 4) return ; //add to result if qualified and then return
if (sec == 4 && pos == s.length()) {
result.add(curIP);
return ;
} //add for length from 1 to 3
for (int i = 1; i <= 3; i++) {
//necessary exit
if (pos + i > s.length()) return ;
String section = s.substring(pos, pos + i);
//corner case, break
if ((section.length() > 1 && section.charAt(0) == '0') || Integer.valueOf(section) >= 256) break;
//add section to curIP
generateIpAddresses(s, sec + 1, pos + i, sec == 0 ? section : curIP + "." + section, result);
}
}
}

93. Restore IP Addresses产生所有可能的ip地址的更多相关文章

  1. 292C Beautiful IP Addresses

    传送门 题目 The problem uses a simplified TCP/IP address model, please read the statement carefully. An I ...

  2. xtu summer individual-4 A - Beautiful IP Addresses

    Beautiful IP Addresses Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on Code ...

  3. 93.Restore IP Addresses(M)

    93.Restore IP Addresses Medium 617237FavoriteShare Given a string containing only digits, restore it ...

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

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

  5. 93. Restore IP Addresses

    题目: Given a string containing only digits, restore it by returning all possible valid IP address com ...

  6. 【LeetCode】93. Restore IP Addresses

    Restore IP Addresses Given a string containing only digits, restore it by returning all possible val ...

  7. LeetCode(93) Restore IP Addresses

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

  8. leetcode 93 Restore IP Addresses ----- java

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

  9. 【一天一道LeetCode】#93. Restore IP Addresses

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

随机推荐

  1. Java面向对象 第5节 抽象类和接口

    一.抽象类和抽象方法 区分抽象方法和普通方法1)当一个方法被abstract修饰时,该方法成为抽象方法2)抽象类所在的类必须定义为抽象类3)抽象方法不会有具体的实现,而是在抽象类的子类中通过方法重写进 ...

  2. django无法同步mysql数据库 Error:1064

    [问题] 具体问题:新建django工程,使用django的manage.py的 migrate命令进行更改. 在初始化数据库表时,失败,错误信息为 django.db.migrations.exce ...

  3. 安装Kerberos后,如何不使用它,Current Kerberos password:

    在不知情的情况下,安装了kerberos,然后只有是有密码的地方,一直有这个: Current Kerberos password: 没有了解过kerberos,想要卸载,卸载了还是有,怎么弄都弄不掉 ...

  4. 乐乐课堂_leleketang.com

    乐乐课堂_leleketang.com https://www.baidu.com/sf?pd=video_page&sign=12394301609542619800&word=抛物 ...

  5. Day 09 函数基础

    函数初级 简介 # 函数是一系列代码的集合,用来完成某项特定的功能 优点 '''1. 避免代码的冗余2. 让程序代码结构更加清晰3. 让代码具有复用性,便于维护''' 函数四部分 '''1. 函数名: ...

  6. C++笔记(1)

    C++笔记1文件与流笔记 参考博客: https://blog.csdn.net/kingstar158/article/details/6859379       关闭文件中: 当文件读写操作完成之 ...

  7. Android jni中回调java的方法

    在上一篇的基础上,添加在C++代码中回调java方法. 代码如下: Demo.java 中添加callback函数, 打印一条log. package com.example.scarecrow.dy ...

  8. Ubuntu 14.10 下安装Ambari

    安装ambari有两种方式,一是自己下载源码编译,另外一个是使用公共仓库 1 使用Public Respositories Step1: Download the Ambari repository ...

  9. mysql识别中文

    在配置的INI中加上这些 [mysql]default-character-set=utf8no-auto-rehash# Remove the next comment character if y ...

  10. 报错:NoSuchMethodError: kafka.javaapi.PartitionMetadata.leader()Lkafka/cluster/Broker;

    报错现象: 在pom文件添加: <dependency> <groupId>org.apache.kafka</groupId> <artifactId> ...