leetcode — restore-ip-addresses
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的更多相关文章
- LeetCode: Restore IP Addresses 解题报告
Restore IP Addresses My Submissions Question Solution Given a string containing only digits, restore ...
- [LeetCode] Restore IP Addresses 复原IP地址
Given a string containing only digits, restore it by returning all possible valid IP address combina ...
- [leetcode]Restore IP Addresses @ Python
原题地址:https://oj.leetcode.com/problems/restore-ip-addresses/ 题意: Given a string containing only digit ...
- LeetCode——Restore IP Addresses
Given a string containing only digits, restore it by returning all possible valid IP address combina ...
- [LeetCode] Restore IP Addresses 回溯
Given a string containing only digits, restore it by returning all possible valid IP address combina ...
- LeetCode Restore IP Addresses
DFS class Solution { public: vector<string> restoreIpAddresses(string s) { return insertDot(s, ...
- leetcode 93. Restore IP Addresses(DFS, 模拟)
题目链接 leetcode 93. Restore IP Addresses 题意 给定一段序列,判断可能组成ip数的所有可能集合 思路 可以采用模拟或者DFS的想法,把总的ip数分成四段,每段判断是 ...
- 【leetcode】Restore IP Addresses
Restore IP Addresses Given a string containing only digits, restore it by returning all possible val ...
- 【LeetCode】93. Restore IP Addresses
Restore IP Addresses Given a string containing only digits, restore it by returning all possible val ...
- 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 ...
随机推荐
- springboot添加邮件发送及压缩功能
springboot添加邮件发送及文件压缩功能 转载请注明出处:https://www.cnblogs.com/funnyzpc/p/9190233.html 先来一段诗 ``` 就这样吧 忍受折磨 ...
- python 多线程 及多线程通信,互斥锁,线程池
1.简单的多线程例子 import threading,timedef b_fun(i): print "____________b_fun start" time.sleep(7 ...
- vue相关文件说明(基于vue2.0)
1.config:生产,开发环境配置参数 2.static:第三方资源,这里面的文件直接写路径,不能用'import'导入 3.node_modules:引入一些依赖包 4..babelrc:定义了E ...
- mysql node
mysql8.0版本 报错:Error: ER_NOT_SUPPORTED_AUTH_MODE: Client does not support authentication protocol req ...
- 漏测BUG借鉴
2. websocket: 用户频繁刷新,后台每次请求新的排队,内存溢出 1. websocket: 北京中心连接正常,外地中心,连接超时,应考虑到外地延迟问题
- webpack2入门概念
webpack是一种JavaScript应用模块化打包工具,它配置起来简单易上手,因此很多企业工程化代码都使用它来打包.在具体介绍如何使用webpack之前,先来介绍下webpack的四个核心概念. ...
- [LeetCode] Positions of Large Groups 大群组的位置
In a string S of lowercase letters, these letters form consecutive groups of the same character. For ...
- web端本地与服务端时间校验
当前校验逻辑: 本地和服务端的时间校验绑定在一个通用请求上,这个请求每七分钟会到服务端请求一次,本地拿到服务器时间后,计算请求服务器来回的时间,最后得出与服务器时间的差值,然后每次new Date() ...
- FFmpeg 学习(四):FFmpeg API 介绍与通用 API 分析
一.FFmpeg 相关术语 1. 容器/文件(Container/File):即特定格式的多媒体文件,比如MP4,flv,mov等. 2. 媒体流(Stream):表示在时间轴上的一段连续的数据,比如 ...
- [Swift]LeetCode274.H指数 | H-Index
Given an array of citations (each citation is a non-negative integer) of a researcher, write a funct ...