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. Exp2 后门原理与实践 20164302 王一帆

    1 实验内容 1.1实验主要内容 (1)使用netcat获取主机操作Shell,cron启动 (0.5分) (2)使用socat获取主机操作Shell, 任务计划启动 (0.5分) (3)使用MSF ...

  2. Numpy 基础运算2

    # -*- encoding:utf-8 -*- # Copyright (c) 2015 Shiye Inc. # All rights reserved. # # Author: ldq < ...

  3. tmux使用中出现的问题和解决方式

    常用操作: tmux ls 看当前都有哪些sessiontmux new -s my1 创建窗口,名为my1ctrl+B,D 退出窗口 (这个就是同时按ctrl和B,然后松开后再按D键)tmux at ...

  4. sketch2code 有的叫screenshot to code什么的

    先mark一下项目,回头再深究 https://github.com/mzbac/sketch2code https://www.floydhub.com/emilwallner/datasets/h ...

  5. sortable的基本属性

    所有的事件回调函数都有两个参数:event和ui,浏览器自有event对象,和经过封装的ui对象   ui.helper - 表示sortable元素的JQuery对象,通常是当前元素的克隆对象    ...

  6. 文本在div中始终垂直居中

    如果是文本字数固定,在div中垂直居中,相信大家都会 这边分享个不固定文本在div中垂直居中的方法 html代码 <div class="box"> <div c ...

  7. [Educational Round 13][Codeforces 678F. Lena and Queries]

    题目连接:678F - Lena and Queries 题目大意:要求对一个点集实现二维点对的插入,删除,以及询问\(q\):求\(max(x\cdot q+y)\) 题解:对每个点集内的点\(P( ...

  8. kvm虚拟机克隆

    1.先关闭被克隆的虚拟机: 2.克隆命令 virt-clone -o 192.168.0.242_sw_web -n 192.168.0.163_nginx -f /data/kvm/images/1 ...

  9. Vue 学习笔记 — filter

    简书 对将要插入html的对象进行处理 一个简单的Vue示例 基本过滤器用法 带参数的过滤器 全局过滤器 (这张图片有点问题,最后显示的应该是 hello world不是null) 过滤器的简单应用 ...

  10. 依赖注入[6]: .NET Core DI框架[编程体验]

    毫不夸张地说,整个ASP.NET Core框架是建立在一个依赖注入框架之上的,它在应用启动时构建请求处理管道过程中,以及利用该管道处理每个请求过程中使用到的服务对象均来源于DI容器.该DI容器不仅为A ...