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 ...
随机推荐
- Ubuntu 18.04 安装 virtualbox
1.安装包下载地址 [https://www.virtualbox.org/wiki/Linux_Downloads] 2.进入软件包的文件夹 sudo dpkg -i 安装包的名字.deb [注]如 ...
- 12、Redis的事务
写在前面的话:读书破万卷,编码如有神 --------------------------------------------------------------------------------- ...
- [Java]Spring框架
在这里学习Spring框架: >>spring&struts框架学习 >>spring >>Java回顾之Spring基础 >>IBM Java ...
- Codeforces Round #374 (Div. 2) D. Maxim and Array 贪心
D. Maxim and Array 题目连接: http://codeforces.com/contest/721/problem/D Description Recently Maxim has ...
- inline关键字的作用
一.在C&C++中,inline关键字用来定义一个类的内联函数,引入它的主要原因是用它替代C中表达式形式的宏定义. 如下面一宏定义表达式: #define express(v1,v2) (v1 ...
- 解决oracle语句中 含数字的字符串按数字排序问题
普通排序利用:order by 字段名 ASC 但是遇到有中文而且类型是varchar类型的结果就是这样 政采代(甲)字第0298号 政采代(甲)字第0421号 政采代(甲)字第1098号 政采代(甲 ...
- [Go] Http包 使用简介
请求的结构 HTTP 的交互以请求和响应的应答模式.Go 的请求我们早就见过了,handler 函数的第二个参数 http.Requests.其结构为: type Request struct { M ...
- Nginx FastCGI的运行原理
http://www.cnblogs.com/yinshoucheng-golden/p/6474034.html
- Dapper-translation 分布式监控系统
http://bigbully.github.io/Dapper-translation/ https://github.com/bigbully/Dapper-translation
- LAMP学习路线图
站点开发概述 LAMP开发概述 HTML基础 CSS基础 DIV+CSS Javascript Jquery(Ajax) WAMP 环境搭建 PHP基本的语法,变量.数据类型,表达式,常量,流程控制, ...