Leetcode(93): Restore IP Addresses
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)
public class Solution {
public List<String> restoreIpAddresses(String s) {
List<String> res=new ArrayList<String>();
if(s.length()<4||s.length()>12) return res;
dfs(s,"",res,0);
return res;
}
public static void dfs(String s,String temp,List<String> list,int count){
if(count==3&&isValid(s)){
list.add(temp+s);
return;
}
for(int i=1;i<4&&i<s.length();i++){
String substr=s.substring(0,i);
if(isValid(substr)){
dfs(s.substring(i),temp+substr+".",list,count+1);
}
}
}
public static boolean isValid(String s){
if(s.charAt(0)=='0') return s.equals("0");
int num=Integer.parseInt(s);
return num<=255&&num>0;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
solution sl=new solution();
List<String> list=sl.restoreIpAddresses(sc.next());
for(String i:list){
System.out.println(i);
}
}
}
Leetcode(93): Restore IP Addresses的更多相关文章
- LeetCode(93): 复原IP地址
Medium! 题目描述: 给定一个只包含数字的字符串,复原它并返回所有可能的 IP 地址格式. 示例: 输入: "25525511135" 输出: ["255.255. ...
- Xilinx Vivado的使用详细介绍(3):使用IP核
ilinx Vivado的使用详细介绍(3):使用IP核 Author:zhangxianhe IP核(IP Core) Vivado中有很多IP核可以直接使用,例如数学运算(乘法器.除法器.浮点运算 ...
- 開玩樹莓派(二):配置IP,實現無顯示器局域網內Putty連接和RDP遠程
目錄: 開玩樹莓派(一):安裝Raspbian系統 開玩樹莓派(二):配置IP,實現無顯示器局域網內Putty連接和RDP遠程 開玩樹莓派(三):Python編程 開玩樹莓派(四):GPIO控制和遠程 ...
- LeetCode(1):两数之和
写在前面:基本全部参考大神“Grandyang”的博客,附上网址:http://www.cnblogs.com/grandyang/p/4130379.html 写在这里,是为了做笔记,同时加深理解, ...
- LeetCode(93) Restore IP Addresses
题目 Given a string containing only digits, restore it by returning all possible valid IP address comb ...
- LeetCode(91):解码方法
Medium! 题目描述: 一条包含字母 A-Z 的消息通过以下方式进行了编码: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 给定一个只包含数字的非空字符串,请计 ...
- LeetCode(90):子集 II
Medium! 题目描述: 给定一个可能包含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 说明:解集不能包含重复的子集. 示例: 输入: [1,2,2] 输出: [ [2], [1 ...
- LeetCode(78):子集
Medium! 题目描述: 给定一组不含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 说明:解集不能包含重复的子集. 示例: 输入: nums = [1,2,3] 输出: [ [3 ...
- LeetCode(3):无重复字符的最长子串
Medium! 题目描述: 给定一个字符串,找出不含有重复字符的 最长子串 的长度. 示例: 给定 "abcabcbb" ,没有重复字符的最长子串是 "abc" ...
随机推荐
- IE edge是怎么了??
IE edge 怎么不能通过$.getJSON(url,function(data){ alert(''); });获取数据呢?,其他浏览器和IE的10以下版本都没问题获取到了,这是什么情况!本来是想 ...
- maven发布项目的snapshot到nexus
1.配置发布地址信息 <repositories> <repository> <id>nexus</id> <name>Local Repo ...
- lofter个人网站文艺愤青下载
lofter地址→点击访问 你妹扫我 生成地址
- 启动windows服务的bat文件编写格式
1.bat文件需要和bin文件内容放在一起 启动服务的bat文件如下: sc create 邮件服务 binPath= "%~dp0可执行文件名称.exe" start= auto ...
- Web容器与Servlet
转自:http://www.360doc.com/content/10/0713/20/495229_38798294.shtml Web服务器与Web应用层属于不容两个范畴,为了让他们两写作,首先应 ...
- Game 游戏开发
Scut:https://git.oschina.net/scutgame/Scut unity3d:http://unity3d.com/get-unity cocos2d-x:http://www ...
- Abp Uow 设计
初始化入口 在AbpKernelModule类中,通过UnitOfWorkRegistrar.Initialize(IocManager) 方法去初始化 /// <summary> /// ...
- 搭建SpringbootAdmin监控中心报错A attempt was made to call the method reactor.retry.Retry.retryMax(I)Lreactor/ret)
遇到了同样的错误,转载记录下: 转载自:https://blog.csdn.net/qq_41938882/article/details/85048953 很明显,还没有启动成功就报错了.报错原 ...
- 华为交换机忘记console的密码,怎么恢复出厂设置
第一步:一般情况下由于密码忘记我们会被阻挡在交换机telnet远程管理界面之外. 第二步:虽然可以尝试console线连接交换机的控制接口,但是很多时候这个密码也被网络管理员进行了设置,不巧的是如果这 ...
- Python自省(反射)指南(转)
原文:http://www.cnblogs.com/huxi/archive/2011/01/02/1924317.html 在笔者看来,自省和反射是一回事,当然其实我并不十分确定一定以及肯定,所以如 ...