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的更多相关文章

  1. LeetCode(93): 复原IP地址

    Medium! 题目描述: 给定一个只包含数字的字符串,复原它并返回所有可能的 IP 地址格式. 示例: 输入: "25525511135" 输出: ["255.255. ...

  2. Xilinx Vivado的使用详细介绍(3):使用IP核

    ilinx Vivado的使用详细介绍(3):使用IP核 Author:zhangxianhe IP核(IP Core) Vivado中有很多IP核可以直接使用,例如数学运算(乘法器.除法器.浮点运算 ...

  3. 開玩樹莓派(二):配置IP,實現無顯示器局域網內Putty連接和RDP遠程

    目錄: 開玩樹莓派(一):安裝Raspbian系統 開玩樹莓派(二):配置IP,實現無顯示器局域網內Putty連接和RDP遠程 開玩樹莓派(三):Python編程 開玩樹莓派(四):GPIO控制和遠程 ...

  4. LeetCode(1):两数之和

    写在前面:基本全部参考大神“Grandyang”的博客,附上网址:http://www.cnblogs.com/grandyang/p/4130379.html 写在这里,是为了做笔记,同时加深理解, ...

  5. LeetCode(93) Restore IP Addresses

    题目 Given a string containing only digits, restore it by returning all possible valid IP address comb ...

  6. LeetCode(91):解码方法

    Medium! 题目描述: 一条包含字母 A-Z 的消息通过以下方式进行了编码: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 给定一个只包含数字的非空字符串,请计 ...

  7. LeetCode(90):子集 II

    Medium! 题目描述: 给定一个可能包含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 说明:解集不能包含重复的子集. 示例: 输入: [1,2,2] 输出: [ [2], [1 ...

  8. LeetCode(78):子集

    Medium! 题目描述: 给定一组不含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 说明:解集不能包含重复的子集. 示例: 输入: nums = [1,2,3] 输出: [ [3 ...

  9. LeetCode(3):无重复字符的最长子串

    Medium! 题目描述: 给定一个字符串,找出不含有重复字符的 最长子串 的长度. 示例: 给定 "abcabcbb" ,没有重复字符的最长子串是 "abc" ...

随机推荐

  1. python--生成测试数据

    1.方法 import datetime params_dict_data = { "system_id":"systemId001", "order ...

  2. SaltStack自动化安装zabbix-server

    使用SaltStack自动化安装zabbix-server 1,设置ntp时间同步 2,安装zabbix-agent 3,安装zabbix-server 4,安装及配置mariadb(mariadb与 ...

  3. HDU_5538_House Building

    House Building Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) ...

  4. [iOS微博项目 - 3.5] - 封装业务

    github: https://github.com/hellovoidworld/HVWWeibo   A.封装微博业务 1.需求 把微博相关业务(读取.写微博) 界面控制器不需要知道微博操作细节( ...

  5. webPage logService 日志服务 剥离

    [旧的场景]1.x.a.com指向负载均衡服务器ipL;2.代码所在的应用服务器ipA,ipB,ipC,运行nginx-phpFPM服务,提供2个服务: 2.1.应用服务器ipA,ipB,ipC中we ...

  6. java.math.BigDecimal

    package day14; import java.math.BigDecimal; /** * Created by admin on 2018/11/24. * 看程序写结果:结果和我们想的有一 ...

  7. 每人涨10%的工资,涨的前一共不超过5万,从低工资往高工资的人涨,超过5W则停止涨,问涨的钱花了多少,多少人获得了涨薪。

    ;with test(CID,money,NewAmount) as ( SELECT Row_Number() over ( order by money ) as CID ,money ,mone ...

  8. MySQL 最基本的SQL语法/语句

    DDL—数据定义语言(Create,Alter,Drop,DECLARE) DML—数据操纵语言(Select,Delete,Update,Insert) DCL—数据控制语言(GRANT,REVOK ...

  9. NodeJS开发环境搭建

    原创作者: zero20121222 NodeJS开发环境搭建 1.安装NodeJS 1.编译环境 源代码编译器,通常 Unix/Linux平台都自带了C++的编译器(GCC/G++).如果没有,请通 ...

  10. Sql order by 和 group BY 如何共同运用?

    如果声明了 GROUP BY 子句,输出就分成匹配一个或多个数值的不同组里. 如果出现了 HAVING 子句,那么它消除那些不满足给出条件的组. 如果声明了 ORDER BY 子句,那么返回的行是按照 ...