[LintCode] Restore IP Address 复原IP地址
Given a string containing only digits, restore it by returning all possible valid IP address combinations.
Given "25525511135", return
[
"255.255.11.135",
"255.255.111.35"
]
Order does not matter.
LeetCode上的原题,请参见我之前的博客Restore IP Addresses。
解法一:
class Solution {
public:
/**
* @param s the IP string
* @return All possible valid IP addresses
*/
vector<string> restoreIpAddresses(string& s) {
vector<string> res;
helper(s, , "", res);
return res;
}
void helper(string s, int k, string out, vector<string>& res) {
if (k == ) {
if (s.empty()) res.push_back(out);
return;
}
for (int i = ; i < ; ++i) {
if (s.size() < i) break;
int val = stoi(s.substr(, i));
if (val > || i != to_string(val).size()) continue;
helper(s.substr(i), k - , out + s.substr(, i) + (k == ? "" : "."), res);
}
}
};
解法二:
class Solution {
public:
/**
* @param s the IP string
* @return All possible valid IP addresses
*/
vector<string> restoreIpAddresses(string& s) {
vector<string> res;
for (int a = ; a < ; ++a)
for (int b = ; b < ; ++b)
for (int c = ; c < ; ++c)
for (int d = ; d < ; ++d)
if (a + b + c + d == s.size()) {
int A = stoi(s.substr(, a));
int B = stoi(s.substr(a, b));
int C = stoi(s.substr(a + b, c));
int D = stoi(s.substr(a + b + c, d));
if (A <= && B <= && C <= && D <= ) {
string t = to_string(A) + "." + to_string(B) + "." + to_string(C) + "." + to_string(D);
if (t.size() == s.size() + ) res.push_back(t);
}
}
return res;
}
};
[LintCode] Restore IP Address 复原IP地址的更多相关文章
- [LeetCode] Restore IP Addresses 复原IP地址
Given a string containing only digits, restore it by returning all possible valid IP address combina ...
- [Leetcode] restore ip address 存储IP地址
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] Validate IP Address 验证IP地址
In this problem, your job to write a function to check whether a input string is a valid IPv4 addres ...
- 华东师大OJ:IP Address【IP地址转换】
/*===================================== IP Address Time Limit:1000MS Memory Limit:30000KB Total Subm ...
- 468 Validate IP Address 验证IP地址
详见:https://leetcode.com/problems/validate-ip-address/description/ Java实现: class Solution { public St ...
- 093 Restore IP Addresses 复原IP地址
给定一个只包含数字的字符串,复原它并返回所有可能的IP地址格式.例如:给定 "25525511135",返回 ["255.255.11.135", " ...
- Leetcode93. Restore IP Addresses复原IP地址
给定一个只包含数字的字符串,复原它并返回所有可能的 IP 地址格式. 示例: 输入: "25525511135" 输出: ["255.255.11.135", ...
- Windows Azure Cloud Service (44) 将Cloud Service加入Virtual Network Subnet,并固定Virtual IP Address(VIP)
<Windows Azure Platform 系列文章目录> 在之前的文章中,笔者已经详细介绍了如何将Virtual Machine加入Virtual Network,并且绑定固定的Pr ...
随机推荐
- PMP 第十一章 项目风险管理
1规划风险管理 2识别风险 3 风险定性分析 4风险定量分析 5规划风险应对 6监控风险 1.项目风险是什么?已知未知风险.未知未知风险对应应急储备和管理储备的关系.风险承受力和风险偏好是什么? 2. ...
- 上四条只是我目前总结菜鸟们在学习FPGA时所最容易跑偏的地
长期以来很多新入群的菜鸟们总 是在重复的问一些非常简单但是又让新手困惑不解的问题.作为管理员经常要给这些菜鸟们普及基础知识,但是非常不幸的是很多菜鸟怀着一种浮躁的心态来学习 FPGA,总是急于求成. ...
- url地址中 "&" "/"等符号的转义处理(转)
URL出现了有+,空格,/,?,%,#,&,=等特殊符号的时候,可能在服务器端无法获得正确的参数值,如何是好? 解决办法:将这些字符转化成服务器可以识别的字符,对应关系如下: URL中的特殊字 ...
- ubuntu kylin中如何截图
windows操作系统中,我通常使用的截图工具是QQ的“ctrl+alt+a”快捷键.但是在ubuntu中,linux qq常年不更新,我也就彻底放弃了使用了,反正ubuntu通常只是拿来开发.其实没 ...
- ios广告
ios广告只需要添加iAd.framework框架 添加广告控件ADBannerView,在控制器中设置广告控件代理<ADBannerViewDelegate>即可,广告会有苹果官方自动推 ...
- SQL作业的操作全
--定义创建作业 转自http://hi.baidu.com/procedure/blog/item/7f959fb10d76f95d092302dd.html DECLARE @jobid uniq ...
- SQL数据库的基本语句
1.修改字段类型语句: alter table 表名 alter column 列名 类型 例如: alter table D alter column no char(15): 2.从其他地方插 ...
- MIT 6.828 JOS学习笔记0. 写在前面的话
0. 简介 操作系统是计算机科学中十分重要的一门基础学科,是一名计算机专业毕业生必须要具备的基础知识.但是在学习这门课时,如果仅仅把目光停留在课本上一些关于操作系统概念上的叙述,并不能对操作系统有着深 ...
- hdu1150 匈牙利
http://acm.split.hdu.edu.cn/showproblem.php?pid=1150 题目大意:有两台机器A和B以及N个需要运行的任务.每台机器有M种不同的模式,而每个任务都恰好在 ...
- DSP using MATLAB 示例Example2.4
n = [0:10]; x = stepseq(0,0,10) - stepseq(10,0,10); [xe,xo,m] = evenodd(x,n); set(gcf,'Color',[1,1,1 ...