93. Restore IP Addresses产生所有可能的ip地址
[抄题]:
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就行了。
[英文数据结构或算法,为什么不用别的数据结构或算法]:
[一句话思路]:
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
- .substring方法是小写
- 需要用到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地址的更多相关文章
- 292C Beautiful IP Addresses
传送门 题目 The problem uses a simplified TCP/IP address model, please read the statement carefully. An I ...
- xtu summer individual-4 A - Beautiful IP Addresses
Beautiful IP Addresses Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on Code ...
- 93.Restore IP Addresses(M)
93.Restore IP Addresses Medium 617237FavoriteShare Given a string containing only digits, restore it ...
- leetcode 93. Restore IP Addresses(DFS, 模拟)
题目链接 leetcode 93. Restore IP Addresses 题意 给定一段序列,判断可能组成ip数的所有可能集合 思路 可以采用模拟或者DFS的想法,把总的ip数分成四段,每段判断是 ...
- 93. Restore IP Addresses
题目: Given a string containing only digits, restore it by returning all possible valid IP address com ...
- 【LeetCode】93. Restore IP Addresses
Restore IP Addresses Given a string containing only digits, restore it by returning all possible val ...
- LeetCode(93) Restore IP Addresses
题目 Given a string containing only digits, restore it by returning all possible valid IP address comb ...
- leetcode 93 Restore IP Addresses ----- java
Given a string containing only digits, restore it by returning all possible valid IP address combina ...
- 【一天一道LeetCode】#93. Restore IP Addresses
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
随机推荐
- APDL link180单元
目录 APDL代码实现link180单元的使用 结果图片 APDL代码实现link180单元的使用 由于不知道怎样使用LINK180单元,故按照相关的教程和理解,整理了一下比较完整的APDL的代码.其 ...
- C# Windows Service 基础
Windows Service这一块并不复杂,但是注意事项太多了,网上资料也很凌乱,偶尔自己写也会丢三落四的.所以本文也就产生了,本文不会写复杂的东西,完全以基础应用的需求来写,所以不会对Window ...
- tomcat接口调用时延开关
项目中有些页面时延不稳定,需要看每次接口调用时延,怎么看,有两种方法:一种是直接去catalina.out日志中看,一种是直接去localhost_access_log日志中看,第一种需要在代码中实现 ...
- freemarker语法介绍及其入门教程实例
# freemarker语法介绍及其入门教程实例 # ## FreeMarker标签使用 #####一.FreeMarker模板文件主要有4个部分组成</br>#### 1.文本,直接输 ...
- RGB格式图像转化为HSV格式
注:在阴影检测算法中经常需要将RGB格式的图像转化为HSV格式,对于阴影区域而言,它的色度和饱和度相对于原图像而言变化不大,主要是亮度信息变化较大,,将RGB格式转化为HSV格式,就可以得到H.S.V ...
- 【剑指offer】合并有序链表
输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则. *思路:假设两个链表的当前结点为n1(list1),n2(list2)比较链表结点值的大小,如果n1.va ...
- 在WINDOWS下安装MYSQL8.0
1:创建文件夹D:\data\service\mysql-8.0.11-winx64\data 2:进到D:\data\service\mysql-8.0.11-winx64\bin 第三步:初始化. ...
- Cookie的存活时间
1. 默认情况下,cookie数据保存到内存里,当浏览器关闭后,Cookie数据被销毁 2. 持久化存储: setMaxAge(int seconds) 1. 正数:将Cookie数据写到硬盘的文件中 ...
- 【转】java面试题
http://blog.csdn.net/jackfrued/article/details/44921941 Java面试题转
- 八(第三篇)、主体结构元素——time元素、pubdate属性
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...