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. jdk12+tomcat9 配置

    jdk12 没有了jre的配置   直接配置path就可以了 tomcat常规配置,只是启动的时候麻烦一点   需要重新写入jdk jre jvm 到server.bat文件 参考文章: https: ...

  2. 解决fastJson无序问题

    对外提供接口,第三方传过来的参数没问题.可是用fastJson 转换的出现 参数顺序不一致,导致 验签失败 解决fastJosn 转换无序问题 https://github.com/alibaba/f ...

  3. 机器学习(九)隐马尔可夫模型HMM

    1.隐马尔可夫HMM模型 一个隐马尔可夫模型可以表示为\[\lambda=\{A,B,\pi\}\]具体就不说了,比较基本. 2.HMM模型的三个基本问题 1.概率计算问题:给定\(\lambda\) ...

  4. jQuery倒计时组件(jquery.downCount.js)

    //html <span class="days">00</span> <span class="hours">00< ...

  5. node.js搭建Web服务器

    Node.js 博客搭建 一. 学习需求 Node 的安装运行 会安装node,搭建node环境 会运行node. 基础模块的使用 Buffer:二进制数据处理模块 Event:事件模块 fs:文件系 ...

  6. JavaScript 获取完整当前域名

    <script> var dq_url = window.location.protocol+"//"+window.location.host;/*获取当前域名*/ ...

  7. ES6新增对象方法的访问描述符:get(只读)、set(只写)

    Es6新增对象方法的访问描述符:get(只读).set(只写),可以直接使用,一般用于数据监听,用途类似于vue.$watch. var obj = { a:1, get bar() { return ...

  8. CUDA编程模型

    1. 典型的CUDA编程包括五个步骤: 分配GPU内存 从CPU内存中拷贝数据到GPU内存中 调用CUDA内核函数来完成指定的任务 将数据从GPU内存中拷贝回CPU内存中 释放GPU内存 *2. 数据 ...

  9. 学用HBuilder开发App的看过来

    自己的呕心沥血之作吧,花了一年时间,系统介绍HTML5 App开发的相关技术. 越来越多的公司采用HTML5来快速开发移动跨平台App,它支持当前市场流行的移动设备. 本书主要介绍了HTML5在移动A ...

  10. 【RL-TCPnet网络教程】第27章 DNS域名系统基础知识

    第27章      DNS域名系统基础知识 本章节为大家讲解DNS(Domain Name System,域名系统),通过前面章节对TCP和UDP的学习,需要大家对DNS也有个基础的认识. (本章的知 ...