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 ...
随机推荐
- HashMap实现原理(转)
来自:http://www.cnblogs.com/xwdreamer/archive/2012/05/14/2499339.html 0.参考文献: hash算法 (hashmap 实现原理) Ja ...
- hdu4711Weather 概率dp
//第i个城市到第j个城市的概率ma[i][j] //第i天的天气天气wet[i] //第i个城市天气为j的概率wet_m[i][j] //Hovey从0点開始,找出其概率最大的路线 //dp[i][ ...
- atitit.http get post的原理以及框架实现java php
atitit.http get post的原理以及框架实现java php 1. 相关的设置 1 1.1. urlencode 1 1.2. 输出流的编码 1 1.3. 图片,文件的post 1 2. ...
- [docker]bind9.11-with-mysql5.6 docker容器化实战
参考: https://www.centos.bz/2012/09/bind-with-mysql-support/ http://blog.51niux.com/?id=125 http://470 ...
- Codeforces Round #297 (Div. 2) 525D Arthur and Walls(dfs)
D. Arthur and Walls time limit per test 2 seconds memory limit per test 512 megabytes input standard ...
- [转]Ubuntu 12.04.3 LTS 安装 Qt5.1.1
参考文档如下: http://blog.csdn.net/astonqa/article/details/9767043 http://www.qtcn.org/bbs/apps.php?q=diar ...
- std::thread “terminate called without an active exception”
最近在使用std::thread的时候,遇到这样一个问题: std::thread t(func); 如果不使用调用t.join()就会遇到 "terminate called whitho ...
- 跟着百度学PHP[15]-session回收机制
gc(Garbage Collection 垃圾回收) 在用户访问的时候会生成许多的临时session文件,顾名思义session回收机制就是用来删除这些临时文件的. session.gc_maxli ...
- 构建自己的embedded linux系统
[教程]使用buildroot完全自定义自己的embedded linux系统(nand)http://www.eeboard.com/bbs/thread-38377-1-1.html [教程] [ ...
- Flex远程访问获取数据--HTTPService
编写service类: package services { import com.adobe.serialization.json.JSON; import log.LogUtil; import ...