【leetcode】Restore IP Addresses (middle)
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)的更多相关文章
- 【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(DFS, 模拟)
题目链接 leetcode 93. Restore IP Addresses 题意 给定一段序列,判断可能组成ip数的所有可能集合 思路 可以采用模拟或者DFS的想法,把总的ip数分成四段,每段判断是 ...
- 【leetcode刷题笔记】Restore IP Addresses
Given a string containing only digits, restore it by returning all possible valid IP address combina ...
- [LeetCode] 93. Restore IP Addresses 复原IP地址
Given a string containing only digits, restore it by returning all possible valid IP address combina ...
- leetcode 93 Restore IP Addresses ----- java
Given a string containing only digits, restore it by returning all possible valid IP address combina ...
- Java for LeetCode 093 Restore IP Addresses
Given a string containing only digits, restore it by returning all possible valid IP address combina ...
- 【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 ...
- LeetCode : 93. Restore IP Addresses
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABZ4AAAHUCAYAAAC6Zj2HAAAMFGlDQ1BJQ0MgUHJvZmlsZQAASImVlw
- 【leetcode】Word Break (middle)
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...
随机推荐
- 在OS X中使用Homebrew
Homebrew可以很方便的进行软件包管理,用官网的一句话来形容就是 Homebrew 使 OS X 更完整.用 gem 来安装您的 gems.用 brew 来搞定它们的依赖包. 安装Homebrew ...
- HDU 1048
#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <string.h> int main() { char ...
- 第一个C++例子
#include <iostream> using namespace std; class Time { private: int hour; int minute; int secon ...
- notepad++和sublime 常用插件及主题
sublime: 常用主题有: Pastels on Dark Monokai Zenburnsque 常用插件有 Anaconda Package Control Side Bar ConvertT ...
- JQuery在asp.net中三种ajax传值
1)通过webservice,注意去掉注释[System.Web.Script.Services.ScriptService]这行前的注释 2)通过aspx.cs文件中的静态方法 3)通过aspx文件 ...
- Android常用的工具类
主要介绍总结的Android开发中常用的工具类,大部分同样适用于Java.目前包括HttpUtils.DownloadManagerPro.ShellUtils.PackageUtils. Prefe ...
- 14个Xcode中常用的快捷键操作(转)
2014-12-24 17:31 编辑: suiling 分类:iOS开发 来源:iPhoneDev.tv 13 31621 Xcode 6Xcode快捷键 招聘信息: 开发工程师 iOS开发工程师 ...
- [BZOJ4326][codevs4632][codevs5440][UOJ#150][NOIP2015]运输计划
[BZOJ4326][codevs4632][codevs5440][UOJ#150][NOIP2015]运输计划 试题描述 公元 2044 年,人类进入了宇宙纪元. L 国有 n 个星球,还有 n− ...
- [转载]JavaEE学习篇之——JQuery技术详解
原文链接:http://blog.csdn.net/jiangwei0910410003/article/details/32102187 1.简介2.工具3.jQuery对象 1.DOM对象转化成j ...
- php批量下载文件
最近用codeigniter开发一个图片网站,发现单文件下载很容易实现,批量下载的话,就有点麻烦. 普通php下载比较简单,比如我封装的一个函数: function shao_download($fi ...