Leetcode-Resotre 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)
Analysis:
This is a recursive problem. A string will be divided into four parts. For each part, we should determine whether it is a valid IP segment.
Solution:
public class Solution {
public List<String> restoreIpAddresses(String s) {
List<String> res = new ArrayList<String>();
if (s.length()==0) return res;
res = restoreRecur(s,0,4);
return res;
}
public List<String> restoreRecur(String s, int curIndex, int num){
List<String> res = new ArrayList<String>();
if (curIndex>=s.length()) return res;
if (num==1){
String temp = s.substring(curIndex,s.length());
if (temp.length()>3) return res;
int val = Integer.parseInt(temp);
if (temp.length()==3 && val>=100 && val<=255){
res.add(temp);
return res;
}
if (temp.length()==2 && val>=10 && val<=99){
res.add(temp);
return res;
}
if (temp.length()==1){
res.add(temp);
return res;
}
return res;
}
if (curIndex+1>=s.length()) return res;
int end = curIndex+3;
if (curIndex+3>s.length())
end = s.length();
for (int i=curIndex+1;i<=end;i++){
String temp = s.substring(curIndex,i);
int val = Integer.parseInt(temp);
List<String> nextRes = new ArrayList<String>();
if (temp.length()==3 && val>=100 && val<=255){
nextRes = restoreRecur(s,i,num-1);
}
if (temp.length()==2 && val>=10 && val<=99){
nextRes = restoreRecur(s,i,num-1);
}
if (temp.length()==1){
nextRes = restoreRecur(s,i,num-1);
}
for (int j=0;j<nextRes.size();j++)
res.add(temp+"."+nextRes.get(j));
}
return res;
}
}
Leetcode-Resotre 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 OJ-- Restore IP Addresses
https://oj.leetcode.com/problems/restore-ip-addresses/ string到int的ip地址格式化. 分别用 i+1,j+1,k+1,表示前三个地址段的 ...
- 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 ...
随机推荐
- golang中使用mongodb的操作类以及如何封装
mgo简介 mongodb官方没有关于go的mongodb的驱动,因此只能使用第三方驱动,mgo就是使用最多的一种. mgo(音mango)是MongoDB的Go语言驱动,它用基于Go语法的简单API ...
- linux 安装 登录 centos7
常用资源下载 r.aminglinux.com centos7.aminglinux.com http://www.apelearn.com/study_v2/ 认识linux Debian Slac ...
- pandas DataFrame 数据处理常用操作
Xgboost调参: https://wuhuhu800.github.io/2018/02/28/XGboost_param_share/ https://blog.csdn.net/hx2017/ ...
- python 2,3版本自动识别导入
import sys if str(sys.version[0]) == "3": from urllib.parse import quote_plus from ...
- PHP REST架构简单设计
REST是什么? REST(Representational State Transfer表述性状态转移)是一种针对网络应用的设计和开发方式,可以降低开发的复杂性,提高系统的可伸缩性. REST的特点 ...
- Java并发编程(九):拓展
java多线程死锁理解 Java多线程并发最佳实践 Spring与线程安全 HashMap与ConcurrentHashMap 关于java集合类HashMap的理解 , 数据结构之 ...
- atitit.流程标准化--- mysql启动不起来的排查流程attilax总结
atitit.流程标准化--- mysql启动不起来的排查流程attilax总结 1. mysql的启动日志文件 1 2. console方式 1 3. 安装为服务 1 3.1. 使用默认配置文件 1 ...
- Java序列化与反序列化学习(一)
一.序列化与反序列化概述 当两个进程在进行远程通信时,彼此可以发送各种类型的数据.无论是何种类型的数据,都会以二进制序列的形式在网络上传送.发送方需要把这个Java对象转换为字节序列,才能在网 ...
- poj Buy Tickets
题目链接:http://poj.org/problem?id=2828 类似的题目:http://www.cnblogs.com/lovychen/p/3674048.html 测试数据: in: 4 ...
- layui基础上的tree菜单动态渲染;
var layout=[ { title:'脚本对象名称', treeNodes:true, headerClass:'value_col', colClass:'value_col', style: ...