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" ...
随机推荐
- java手写的动态数组JimisunArray
/** * @Author:jimisun * @Description: * @Date:Created in 22:10 2018-07-18 * @Modified By: */ public ...
- 170407、java基于nio工作方式的socket通信
客户端代码: /** * */ package com.bobohe.nio; import java.io.BufferedReader; import java.io.IOException; i ...
- JAVAWEB基础模块开发顺序与数据访问对象实现类步骤
一.模块的开发的顺序 1. 定义数据表 2. 新建模型类 3. 新建"add.jsp" 4. 实现AddServlet中的doGet()方法 5. 定义Dao.Service接口 ...
- CentOS设置密码复杂度及过期时间等
我们在使用linux系统设置密码的时候,经常遇到这样的问题,系统提示:您的密码太简单,或者您的密码是字典的一部分.那么系统是如何实现对用户的密码的复杂度的检查的呢? 系统对密码的控制是有两部分(我知道 ...
- 直接IO 零拷贝 DAM 自缓存应用程序
直接IO 零拷贝 DAM 自缓存应用程序
- window.navigator.userAgent $_SERVER['HTTP_USER_AGENT']
wjs php返回结果一致 <script> !function () { var UA = window.navigator.userAgent, docEl = document.do ...
- TestLink安装手册
环境准备 系统CentOS Linux release 7.3.1611 (Core) 搭建LAMP所需的集成包 xampp-linux-x64-7.2.0-0-installer.run 下载地址 ...
- pendingIntent的FLAG标签:
PendingIntent是一个特殊的Intent,实际上它像一个邮包,其中包裹着真正的Intent,当邮包未打开时,Intent是被“挂起”的,所以并不执行, 只有当邮包拆开时才会执行.它与Inte ...
- filebeat配置不同路径下的log的两种方法
第一种方法: vim /etc/filebeat/filebeat.yml filebeat.inputs: # Each - is an input. Most options can be set ...
- awk经常使用字符串处理函数
gsub(regexp, replacement [, target]) Search target for all of the longest, leftmost, nonoverlapping ...