给定一个只包含数字的字符串,复原它并返回所有可能的IP地址格式。
例如:
给定 "25525511135",
返回 ["255.255.11.135", "255.255.111.35"]. (我们可以不考虑数组内元素的顺序)
详见:https://leetcode.com/problems/restore-ip-addresses/description/

Java实现:

class Solution {
public List<String> restoreIpAddresses(String s) {
List<String> res = new ArrayList<String>();
if (s.length()<4||s.length()>12){
return res;
}
String item = new String();
helper(s, 0, item, res);
return res;
} private void helper(String s, int start, String item, List<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)){
helper(s.substring(i+1), start+1, item + substr + '.', res);
}
}
} private boolean isValid(String s){
if (s.charAt(0)=='0'){
return s.equals("0");
}
int num = Integer.parseInt(s);
if(num > 0 && num <= 255){
return true;
}else{
return false;
}
}
}

参考:https://www.cnblogs.com/springfor/p/3886409.html

093 Restore IP Addresses 复原IP地址的更多相关文章

  1. [LeetCode] Restore IP Addresses 复原IP地址

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

  2. [LeetCode] 93. Restore IP Addresses 复原IP地址

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

  3. Leetcode93. Restore IP Addresses复原IP地址

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

  4. [LintCode] Restore IP Address 复原IP地址

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

  5. 93. Restore IP Addresses

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

  6. 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 ...

  7. leetcode -day29 Binary Tree Inorder Traversal &amp; Restore IP Addresses

    1.  Binary Tree Inorder Traversal Given a binary tree, return the inorder traversal of its nodes' ...

  8. 【leetcode】Restore IP Addresses

    Restore IP Addresses Given a string containing only digits, restore it by returning all possible val ...

  9. 【一天一道LeetCode】#93. Restore IP Addresses

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

随机推荐

  1. IE浏览器没有加载CSS或js文件的秘密及解决办法

    其实是两处资料拼成这一篇博文的,因为在开发过程中遇到,有的文章只是说明原因,而没有给出解决方案,所以再次给出解释和解决方法,以供参考,如果有好的解决方法,也请分享下! ---------------- ...

  2. rc.local 开启自启动,检测是否成功

    rc.local /etc/init.d/nginx start 查看运行状态 systemctl status rc-local ● rc-local.service - /etc/rc.local ...

  3. android vector pathData探究,几分钟绘制自己的vectordrawable

    之前经常看到一些酷酷的图标效果, 深入进去发现不是直接用的图片, 而是一些以Vector标签开头的xml文件, 于是就看到了如下代码: <vector xmlns:android="h ...

  4. 看 迪杰斯特拉(Dijsktra)算法体会

    迪杰斯特拉 看啊哈算法中迪杰斯特拉算法体会: 算法思路 : 1.先找到源头到其他点的最短路: 2.以最短路作为中转点进行比较,用一个dis数组保存源头到他的最优距离 3.用循环进行最优筛选: #inc ...

  5. 多线程辅助类-CountDownLatch的用法

    转自:http://www.iteye.com/topic/1002652 CountDownLatch,一个同步辅助类,在完成一组正在其他线程中执行的操作之前,它允许一个或多个线程一直等待. 主要方 ...

  6. 为什么在启动linux后进入终端提示sh-3.2#

    这是Linux系统环境变量设置问题,用户登陆后确保是root权限,可以用如下这两条命令解决:-bash-3.2# cp /etc/skel/.{bash_profile,bashrc} ~-bash- ...

  7. codevs 1046 旅行家的预算

    传送门 1046 旅行家的预算 1999年NOIP全国联赛普及组NOIP全国联赛提高组  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold题解   题目描述 Des ...

  8. Mysql源码学习——Thread Manager

    一.前言 上篇的Connection Manager中,曾提及对于一个新到来的Connection,服务器会创建一个新的线程来处理这个连接. 其实没那么简单,为了提高系统效率,减少频繁创建线程和中止线 ...

  9. Azure Key Vault (3) 在Azure Windows VM里使用Key Vaule

    <Windows Azure Platform 系列文章目录> 本章我们介绍如何在Azure Windows VM里面,使用.NET使用Azure Key Vault 我们需要对Key V ...

  10. heartbeat3.x部署安装

    使用Heartbeat构建Linux双机热备系统 本文档版本号: V1.0 版 本 历 史 版本号 更新时间 说 明 创建者 V1.0 2013-3-23 修改版 金桥 1 部署环境 OS: Red ...