【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 ...
随机推荐
- 基于AngularJS的过滤与排序
前面了解了AngularJS的使用方法,这里就简单的写个小程序,实现查询过滤以及排序的功能. 本程序中可以了解到: 1 angularjs的过滤器 2 ng-repeat的使用方法 3 控制器的使用 ...
- UIGestureRecognizer
•为了完成手势识别,必须借助于手势识别器----UIGestureRecognizer • •利用UIGestureRecognizer,能轻松识别用户在某个view上面做的一些常见手势 • •UIG ...
- linux下系统对于sigsegv错误时的处理
一般来讲,对非法地址的访问会导致应用程序收到由系统发送的sigsegv信号,默认情况下,函数对于这个信号的处理是退出. 但是为了方便调试,我们可以自己设置处理函数,使用signal函数. 这里比较重要 ...
- 清除浮动(clearfix hack)
eg:
- linux 脚本命令匹配并获取下一行数据
三种方式: 匹配“Title”并打印出匹配行的下一行 grep -A 1 'Title' urfile awk '/Title/{getline a;print $0"\n"a ...
- Javascript高级程序设计——基本概念(二)
相等操作符: 相等==:这个操作符会先转换操作数,强制类型转换,然后再比较他们的相等性. null == undefined //true NaN == NaN //false"5" ...
- Redis学习笔记八:独立功能之二进制位数组
Redis 提供了 setbit.getbit.bitcount.bitop 四个命令用于处理二进制位数组. setbit 命令用于为位数组指定偏移量上的二进制位设置值,偏移量从 0 开始计数. ge ...
- Nginx的作用
负载均衡 现在几乎看不到单机奋斗的应用了吧.反向代理服务器可以根据负载均衡算法进行均匀或者自定义的转发.常见的负载均衡算法有:轮转算法(Round Robin).最少连接算法(Least Connec ...
- Markdown入门教程
Markdown 是一种轻量级的「标记语言」,它的优点很多,目前也被越来越多的写作爱好者,撰稿者广泛使用.看到这里请不要被「标记」.「语言」所迷惑,Markdown 的语法十分简单.常用的标记符号也不 ...
- vSphere、Hyper-V与XenServer 你选哪个?
vSphere.Hyper-V与XenServer 你选哪个? 当在VMware vSphere.Microsoft Hyper-V与Citrix Systems XenServer之间做出选择时,v ...