import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List; /**
* Source : https://oj.leetcode.com/problems/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 RestoreIpAddress { public void recursion (String str, int index, int count, String ipStr, List<String> result) {
if (index == str.length()) {
if (count == 4) {
result.add(ipStr.substring(0, ipStr.length()-1));
}
return;
} // ip由四个数字组成
if (count == 4) {
return;
} for (int i = index; i < str.length() && i < index + 3 ; i++) {
// ip最大是255,如果是三位数第一位最大只能是2
if (i - index > 1 && str.charAt(index) > '2') {
return;
}
ipStr += str.substring(index, i+1) + ".";
recursion(str, i+1, ++count, ipStr, result);
ipStr = ipStr.substring(0, ipStr.length() - (i - index + 2));
count --;
}
} public List<String> restore (String str) {
if (str.length() < 4) {
return Collections.EMPTY_LIST;
}
List<String> result = new ArrayList<String>();
String ipStr = "";
recursion(str, 0, 0, ipStr, result);
return result;
} public static void main(String[] args) {
RestoreIpAddress restoreIpAddress = new RestoreIpAddress();
List<String> list = restoreIpAddress.restore("25525511135");
System.out.println(Arrays.toString(list.toArray(new String[list.size()]))); list = restoreIpAddress.restore("11111");
System.out.println(Arrays.toString(list.toArray(new String[list.size()]))); list = restoreIpAddress.restore("19813713411");
System.out.println(Arrays.toString(list.toArray(new String[list.size()]))); list = restoreIpAddress.restore("");
System.out.println(Arrays.toString(list.toArray(new String[list.size()])));
}
}

leetcode — restore-ip-addresses的更多相关文章

  1. LeetCode: Restore IP Addresses 解题报告

    Restore IP Addresses My Submissions Question Solution Given a string containing only digits, restore ...

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

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

  3. [leetcode]Restore IP Addresses @ Python

    原题地址:https://oj.leetcode.com/problems/restore-ip-addresses/ 题意: Given a string containing only digit ...

  4. LeetCode——Restore IP Addresses

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

  5. [LeetCode] Restore IP Addresses 回溯

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

  6. LeetCode Restore IP Addresses

    DFS class Solution { public: vector<string> restoreIpAddresses(string s) { return insertDot(s, ...

  7. leetcode 93. Restore IP Addresses(DFS, 模拟)

    题目链接 leetcode 93. Restore IP Addresses 题意 给定一段序列,判断可能组成ip数的所有可能集合 思路 可以采用模拟或者DFS的想法,把总的ip数分成四段,每段判断是 ...

  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

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

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

随机推荐

  1. ABAQUS/CAE——Context

    Part(部分) 用户在Part单元内生成单个部件,可以直接在ABAQUS/CAE环境下用图形工具生成部件的几何形状,也可以从其他的图形软件输入部件.详细可参考ABAQUS/CAE用户手册第15章. ...

  2. Hive表生成函数explode讲解

    Hive中的表分析函数接受零个或多个输入,然后产生多列或多行输出. 1.explode函数 explode函数以array类型数据输入,然后对数组中的数据进行迭代,返回多行结果,一行一个数组元素值 A ...

  3. Java实现生产者与消费者模式

    生产者不断向队列中添加数据,消费者不断从队列中获取数据.如果队列满了,则生产者不能添加数据:如果队列为空,则消费者不能获取数据.借助实现了BlockingQueue接口的LinkedBlockingQ ...

  4. React(九)create-react-app创建项目 + 按需加载Ant Design

    (1)create-react-app如何创建项目我前面第一章介绍过了,这里就不过多写了, (2)我们主要来说说按需加载的问题 1. 引入antd npm install antd --save 2. ...

  5. [LeetCode] Flipping an Image 翻转图像

    Given a binary matrix A, we want to flip the image horizontally, then invert it, and return the resu ...

  6. 使用AOP实现方法执行时间和自定义注解

    环境:IDEA2018+JDK1.8+SpringBoot 第一步:在pom文件中引入依赖(度娘有很多(*^▽^*)): <!--引入AOP的依赖--><dependency> ...

  7. topic的leader显示为none的解决办法

    1.查看kafka的topic详细信息 bin/kafka-topics.sh --zookeeper 127.0.0.1:2181 --topic test --describe 配置delete. ...

  8. 操作XML

    别人已经写过很好的XML辅助类,可以直接引用后使用: 我这里自己写一个xml的操作类,目前能实现的是对一个不含集合的类可以操作,含集合的类无法将集合里的数据读取出来, 首先定义一个XML特性,用于区分 ...

  9. nova boot from volume代码分析

    首先要创建一个bootable volume curl -i http://16.158.166.197:8776/v1/c24c59846a7f44538d958e7548cc74a3/volume ...

  10. 【XSS】利用 onload 事件监控流量劫持

    说到跨站资源监控,首先会联想到『Content Security Policy』.既然 CSP 好用,我们何必自己再搞一套呢.那就先来吐槽下 CSP 的缺陷. 目前的 CSP 日志不详细 用过 CSP ...