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 ...
随机推荐
- bzoj5293: [Bjoi2018]求和
题目链接 bzoj5293: [Bjoi2018]求和 题解 暴力 对于lca为1的好坑啊.... 代码 #include<cmath> #include<cstdio> #i ...
- java判断集合是否重复的一种便捷方法
内容来自其它网站,感谢原作者! import java.util.ArrayList; import java.util.HashSet; import java.util.List; /** * 通 ...
- consul vs etcd3
https://sysadmin.libhunt.com/project/etcd/vs/consul
- IAR EWARM : Debugging with CMSIS-DAP
- [Go] 命令行参数解析包(flag 包)使用详解
Go 的 flag 包可以解析命令行的参数. 一.命令行语法 命令行语法主要有以下几种形式: cmd -flag // 只支持bool类型 cmd -flag=xxx cmd -flag ...
- 【DevOps】谁说大象不能跳舞?
作者:范军 (Frank Fan) 新浪微博:@frankfan7 微信:frankfan7 很多企业,尤其是大企业在产品开发和运维上存在着一些普遍问题,比如开发周期长.人员合作程度不高.开发和运 ...
- unlocked_ioctl和compat_ioctl
参考: https://www.cnblogs.com/super119/archive/2012/12/03/2799967.html https://lwn.net/Articles/119652 ...
- iOS 字符串 中包含 % 百分号的方法
百分号的转换,NSString中需要格式化的字符串中百分号使用%%表示,而char*中百分号也是使用%%表示. 例如:NSLog(@"%%%@%%",@"hello&qu ...
- Linux学习18-gitlab新建项目提交代码
前言 gitlab前面已经搭建好了,如果我们想用把代码上传到gitlab仓库上的话,先要新建一个项目仓库.然后本地安装git环境,就可以提交了 root用户 gitlab首次在浏览器上打开web页面, ...
- Linux学习4-阿里云服务器(CentOS)下使用 Tomcat安装 Jenkins
前言 通常做自动化测试,需要用到jenkins来做持续集成,那么linux系统里面如何使用tomcat安装Jenkins环境呢? 前面一篇已经搭建好java和tomcat环境,接着直接下载jenkin ...