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 ...
随机推荐
- jquery固定表头和列头
1.对网上的开源方法稍作了些修改 <script type="text/javascript">// <![CDATA[ function FixTable(Ta ...
- Android SQLite数据库升级,怎么做(事物更改)
SQLiteOpenHelper // 如果数据库文件不存在,只有onCreate()被调用(该方法在创建数据库时被调用一次) public abstract void onCreate(SQLite ...
- 安装selenium
步骤: 1.下载setuptools2.点击安装setuptools.exe3.安装成功后,python的安装目录下会有Scripts的文件夹4.配置环境变量:python安装目录\Scripts5. ...
- HBase shell scan 过滤器用法总结
比较器: 前面例子中的regexstring:2014-11-08.*.binary:\x00\x00\x00\x05,这都是比较器.HBase的filter有四种比较器: (1)二进制比较器:如’b ...
- 使用Spring Aop自定义注解实现自动记录日志
百度加自己琢磨,以下亲测有效,所以写下来记录,也方便自己回顾浏览加深印象之类,有什么问题可以评论一起解决,不完整之处也请大佬指正,一起进步哈哈(1)首先配置文件: <!-- 声明自动为sprin ...
- iOS UILabel 文字 置顶/置底 实现
iOS UILabel控件默认文字位置是居中的,如图所示: 但是我们经常碰到这样的需求,希望文字向上置顶,或者向下置底,但是很遗憾,iOS API中并没有提供相应的属性和方法,需要我们手动设置. 利用 ...
- 解决java.lang.IllegalArgumentException: No converter found for return value of type
1原因:这是因为springmvc默认是没有对象转换成json的转换器的,需要添加jackson依赖. 简而言之 需要将对象转化为json对象 Jackson 是一种实现方式 <depe ...
- mysql的必知技巧
1.使用联合索引可以大大减少查询数据,联合索引的顺序尽量为查询的顺序
- Python开发虚拟环境使用virtualenvwrapper的搭建及pycharm链接步骤
virtualenv 是一个创建隔绝的Python环境的工具.virtualenv创建一个包含所有必要的可执行文件的文件夹,用来使用Python工程所需的包.创建的环境是独立的,互不干扰,无需sudo ...
- [Swift]LeetCode532. 数组中的K-diff数对 | K-diff Pairs in an Array
Given an array of integers and an integer k, you need to find the number of unique k-diff pairs in t ...