Restore IP Addresses leetcode java
题目:
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)
题解:
利用循环递归解决子问题。对于每个段内数来说,最多3位最少1位,所以在每一层可以循环3次,来尝试填段。因为IP地址最多4个分段,当层数是3的时候说明已经尝试填过3个段了,那么把剩余没填的数段接到结尾即可。
这个过程中要保证的是填的数是合法的,最后拼接的剩余的数也是合法的。
注意开头如果是0的话要特殊处理,如果开头是0,判断整个串是不是0,不是的话该字符就是非法的。因为001,01都是不对的。
代码如下:
1 public ArrayList<String> restoreIpAddresses(String s) {
2 ArrayList<String> res = new ArrayList<String>();
3 String item = new String();
4 if (s.length()<4||s.length()>12)
5 return res;
6
7 dfs(s, 0, item, res);
8 return res;
9 }
public void dfs(String s, int start, String item, ArrayList<String> res){
if (start == 3 && isValid(s)) {
res.add(item + s);
return;
}
for(int i=0; i<3 && i<s.length()-1; i++){
String substr = s.substring(0,i+1);
if (isValid(substr))
dfs(s.substring(i+1, s.length()), start+1, item + substr + '.', res);
}
}
public boolean isValid(String s){
if (s.charAt(0)=='0')
return s.equals("0");
int num = Integer.parseInt(s);
if(num <= 255 && num > 0)
return true;
else
return false;
}
Refrence:http://blog.csdn.net/u011095253/article/details/9158449
Restore IP Addresses leetcode java的更多相关文章
- Restore IP Addresses -- LeetCode
原题链接: http://oj.leetcode.com/problems/restore-ip-addresses/ 这道题的解法很接近于NP问题.也是採用递归的解法. 基本思路就是取出一个合法的 ...
- LeetCode: Restore IP Addresses 解题报告
Restore IP Addresses My Submissions Question Solution Given a string containing only digits, restore ...
- leetcode 93. Restore IP Addresses(DFS, 模拟)
题目链接 leetcode 93. Restore IP Addresses 题意 给定一段序列,判断可能组成ip数的所有可能集合 思路 可以采用模拟或者DFS的想法,把总的ip数分成四段,每段判断是 ...
- 【leetcode】Restore IP Addresses
Restore IP Addresses Given a string containing only digits, restore it by returning all possible val ...
- 【LeetCode】93. Restore IP Addresses
Restore IP Addresses Given a string containing only digits, restore it by returning all possible val ...
- 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 22. Generate Parentheses Restore IP Addresses (*) 131. Palindrome Partitioning
backtracking and invariant during generating the parathese righjt > left (open bracket and cloas ...
- leetcode -day29 Binary Tree Inorder Traversal & Restore IP Addresses
1. Binary Tree Inorder Traversal Given a binary tree, return the inorder traversal of its nodes' ...
- 93. Restore IP Addresses
题目: Given a string containing only digits, restore it by returning all possible valid IP address com ...
随机推荐
- BZOJ.4939.[Ynoi2016]掉进兔子洞(莫队 bitset 分组询问)
BZOJ 洛谷 删掉的数即三个区间数的并,想到bitset:查多个区间的数,想到莫队. 考虑bitset的每一位如何对应每个数的不同出现次数.只要离散化后不去重,每次记录time就可以了. 但是如果对 ...
- BZOJ.3058.四叶草魔杖(Kruskal 状压DP)
题目链接 \(2^{16}=65536\),可以想到状压DP.但是又有\(\sum A_i\neq 0\)的问题.. 但是\(2^n\)这么小,完全可以枚举所有子集找到\(\sum A_i=0\)的, ...
- CentOS增加用户到sudo用户组
1.直接增加到wheel用户组 usermod -a -G wheel username 2.编辑/etc/sudoers文件增加 echo "username ALL=(ALL) ALL& ...
- github view source
https://insight.io/ http://www.cnplugins.com/devtool/octotree/
- 读写文件:每次读入大文件里的一行、读写.CSV文件
读文件: 传统的读法.所有读出,按行处理: fp=open("./ps.txt", "r"); alllines=fp.readlines(); fp.clos ...
- ubuntu的配置文件
ubuntu的配置文件 是 ~/.gconf 我是把终端弄挂了, 只能再桌面系统下找到 ~/.gconf 下的相应文件 修改后就恢复到原来状态.
- DirectX全屏游戏中弹出窗口(转)
一直有人问如何在DirectX全屏游戏中弹出窗口就象金山游侠一样.我答应过要给出原码,只是一直没有时间整理,不过现在总算是弄玩了.代码不长,大致作了些注释,但愿你能看懂:)按照我的说明一步步作应该就能 ...
- C#编程(六十三)----------并行LINQ
并行LINQ .NET4在System.Linq命名空间中包含一个新类ParallelEnumerable,可以分解查询的工作使其分布在多个线程上.尽管Enumerable类给IEnumerable& ...
- 设计模式之代理模式之二(Proxy)
from://http://www.cnblogs.com/xwdreamer/archive/2012/05/23/2515306.html 设计模式之代理模式之二(Proxy) 0.前言 在前 ...
- ASP.NET MVC:Form Authentication 相关的学习资源
看完此图就懂了 看完下面文章必须精通 Form authentication and authorization in ASP.NET Explained: Forms Authentication ...