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. 【转载】android中.9png

    在Android的设计过程中,为了适配不同的手机分辨率,图片大多需要拉伸或者压缩,这样就出现了可以任意调整大小的一种图片格式“.9.png”.这种图片是用于Android开发的一种特殊的图片格式,它的 ...

  2. [译]git log

    git log git log命令用来显示提交的快照. 能列出来你项目的历史, 能过滤和搜索你指定的一些修改. git status能让你检查工作目录和stage区的状态, git log只提供被co ...

  3. maven打包步骤_maven 构建项目

    maven打包 1:先在pom文件中添加下面配置  jar <build>        <plugins>            <!-- compiler插件, 设定 ...

  4. Linux(Ubuntu)安装并破解 SecureFX

    根据Linux(Ubuntu)下面SecureCRT 完全破解(地址:http://www.boll.me/archives/680) 添加了Linux(Ubuntu)下面SecureFX 完全破解, ...

  5. 基于Windows10安装Ubuntu双系统

    步骤: 1.从Ubuntu的官网上下载Ubuntu的iSO安装包. http://www.ubuntu.com/download/ 我安装的版本是Ubuntu 14.04.3 LTS 64位版本 2. ...

  6. Java Io(数据输入输出流)

    Java Io 字节流中的DataInputStream 和 DataOutputStream,使用流更加方便,是流的一个扩展,更方便读取int, long,字符等类型数据. 事例代码如下: pack ...

  7. hiho #1305 区间求差

    #1305 : 区间求差 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 给定两个区间集合 A 和 B,其中集合 A 包含 N 个区间[ A1, A2 ], [ A3,  ...

  8. caffe学习系列(6):其他层介绍

    主要包括softmax-loss层(与softmax有区别),全连接层(Inner Prouduct),accuracy层,reshape层, Dropout层. softmax: layers { ...

  9. 微博地址url(id)与mid的相互转换 Java版

    原理: 新浪微博的URL都是如:http://weibo.com/2480531040/z8ElgBLeQ这样三部分. 第一部分(绿色部分)为新浪微博的域名,第二部分(红色部分)为博主Uid,第三部分 ...

  10. MorkDown 常用语法总结

    推荐一款很好用的markdown编辑器:http://www.typora.io/ 基本技巧: 代码高亮 如果你只想高亮语句中的某个函数名或关键字,可以使用``实现 通常编辑器根据diamagneti ...