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)

思路:回溯法 解向量X={str0, str1, str2, str3} 分别是IP地址的四段

判断每个解向量的部分时,先求出该部分最长和最短长度(根据后面每段最少1位,最多3位),回溯求解。符合条件时就把X按照要求的格式压入ans.

注意,判断每个部分是否有效时要排除00,01,001...等0开头的情况。

#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
using namespace std; class Solution {
public:
vector<string> restoreIpAddresses(string s) {
vector<string> ans;
int len = s.length();
if(len < )
return ans; vector<string> X(, "");
vector<vector<string>> S(, vector<string>());
int k = ;
int maxlen = min(, len - * );
int minlen = max(, len - * );
for(int i = minlen; i <= maxlen; i++)
{
string sub = s.substr(, i);
if(isVaildIP(sub))
{
S[].push_back(sub);
}
} while(k >= )
{
while(!S[k].empty())
{
X[k] = S[k].back();
S[k].pop_back();
if(k < )
{
k = k + ;
int startloc = ;
for(int j = ; j < k; j++)
{
startloc += X[j].length();
}
int maxlen = min(, len - ( - k - ) * - startloc);
int minlen = max(, len - ( - k - ) * - startloc);
for(int i = minlen; i <= maxlen; i++)
{
string sub = s.substr(startloc, i);
if(isVaildIP(sub))
{
S[k].push_back(sub);
}
}
}
else
{
ans.push_back(X[]);
for(int i = ; i < ; i++)
{
ans.back().append(".");
ans.back().append(X[i]);
}
}
}
k--;
} return ans;
} bool isVaildIP(string snum)
{
if(snum.size() > && snum[] == '')
return false;
int num = atoi(snum.c_str());
return (num >= && num <= );
}
}; int main()
{
Solution s;
string num =/* "25525511135"*/"";
vector<string> ans = s.restoreIpAddresses(num); return ;
}

【leetcode】Restore IP Addresses (middle)的更多相关文章

  1. 【leetcode】Restore IP Addresses

    Restore IP Addresses Given a string containing only digits, restore it by returning all possible val ...

  2. leetcode 93. Restore IP Addresses(DFS, 模拟)

    题目链接 leetcode 93. Restore IP Addresses 题意 给定一段序列,判断可能组成ip数的所有可能集合 思路 可以采用模拟或者DFS的想法,把总的ip数分成四段,每段判断是 ...

  3. 【leetcode刷题笔记】Restore IP Addresses

    Given a string containing only digits, restore it by returning all possible valid IP address combina ...

  4. [LeetCode] 93. Restore IP Addresses 复原IP地址

    Given a string containing only digits, restore it by returning all possible valid IP address combina ...

  5. leetcode 93 Restore IP Addresses ----- java

    Given a string containing only digits, restore it by returning all possible valid IP address combina ...

  6. Java for LeetCode 093 Restore IP Addresses

    Given a string containing only digits, restore it by returning all possible valid IP address combina ...

  7. 【leetcode】Reorder List (middle)

    Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must do thi ...

  8. LeetCode : 93. Restore IP Addresses

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABZ4AAAHUCAYAAAC6Zj2HAAAMFGlDQ1BJQ0MgUHJvZmlsZQAASImVlw

  9. 【leetcode】Word Break (middle)

    Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...

随机推荐

  1. 迷你版Deferred

    直接贴代码: /** * 迷你版的deferred */ function Deferred(func) { if (this instanceof Deferred === false) { ret ...

  2. 构建spring+mybatis+redis架构时遇到的小异常

    异常一: Caused by: java.lang.NoSuchMethodError: org.springframework.beans.BeanUtils.instantiateClass(Lj ...

  3. 网站SEO优化之添加Sitemap文件。

    Sitemap.xml 故名思意就是站点地图文件,可以指引Google spider 收录相应网页.正确地使用Google Sitemap,可以确保让Google spider 不遗漏网站内的任何页面 ...

  4. 必须知道的.net(继承)

    1.继承定义:就是面向对象中类与类之间的一种关系.通过继承,使得子类具有父类的属性和方法,同时子类也可以通过加入新的属性和方法或者修改父类的属性和方法建立新的类层次. 2.CLR支持实现单继承和接口多 ...

  5. PHP截取中文无乱码函数——cutstr

    转载:http://blog.sina.com.cn/s/blog_694c144f010179wj.html 真正好用的PHP截取中文无乱码函数——cutstr (2012-07-09 11:17: ...

  6. Back to Edit Distance(LCS + LIS)

    Given 2 permutations of integers from 1 to N, you need to find the minimum number of operations nece ...

  7. nginx反向代理proxy模块相关参数

    http_proxy_module Proxy_pass proxy_pass指令属于ngx_http_proxy_module模块,此模块可以将请求转发到另一台服务器:官方说明:http://ngi ...

  8. 5分钟教你Windows 10中将“运行”固定到开始菜单

    导读 “运行”功能深受很多资深IT之家用户喜爱,因为它简约.方便.实用.在Win7等旧版系统中,用户可以让该功能直接在开始菜单显示,方便操作.但在Win10中,由于开始菜单已经重新编写,原有的设定已经 ...

  9. XAMPP端口占用启动不了

    skype默认会占用80和443端口 如果在apache之前启动skype,apache就会启动不了了!! 解决办法很简单: 1. 先启动apache再启动skype,这样skype就会换其他的端口监 ...

  10. ajaxfileupload回到json带<pre>

    ajaxfileupload返回json带<pre> 老系统,将文件上传方式修改为ajax上传,调用ajaxfileupload.js 出错现象: 文件正常提交,后台接收正常,action ...