resotreIpAddress
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)
Subscribe to see which companies asked this question
#include <string>
#include <vector>
using namespace std;
class Solution {
public:
vector<string> restoreIpAddresses(string s) {
vector<string> result;
vector<int> path;
dfs(s,,,path,result);
return result;
}
private:
void dfs(string& s,int start,int num,
vector<int>& path,vector<string>& result){
if(num == ) {
string str = s;
for(int i=;i<;i++){
str.insert(path[i]+i,,'.');
}
result.push_back(str);
return;
} for(int i=;i<;i++){
if((start+i)<s.length() && -num<=s.substr(start+i).length() && s.substr(start+i).length()<=(-num)*){
if(stoi(s.substr(start,i))<=){
if(i> && s[start] =='') break;
if(num== && stoi(s.substr(start+i))>) continue;
if(num== && s.substr(start+i).length()> && s[start+i]=='') continue; if(path.empty()) path.push_back(i);
else path.push_back(path.back()+i);
dfs(s,start+i,num+,path,result);
path.pop_back();
}
}
}
}
};
int main(){
string IPaddress="";
Solution s;
vector<string> result;
result = s.restoreIpAddresses(IPaddress);
return ;
}
resotreIpAddress的更多相关文章
随机推荐
- Ubuntu下Jenkins(docker)配置Docker远程启动
背景: 在做用Jenkins构建docker的操作,需要用Jenkins调用docker命令,需要先安装docker-build-step插件,然后开启docker远程访问. 默认情况下,Docker ...
- [node.js]express+mongoose+mongodb的开发笔记
时间过得很快,6月和7月忙的不可开交,糟心的事儿也是不少,杭州大连来回飞,也是呵呵. 希望下个阶段能沉浸下来,接着学自己想学的.记一下上几周用了几天时间写的课设.因为课设的缘故,所以在短时间里了解下e ...
- 回去看linux的指令2
SYNC CL : MSM8953 @ CL#:12212299 PROJECT PATH : // Platform / N / NILE / COMBINATION / MSM8953 Cross ...
- java中继承的关系
当有父子关系的两个类(继承关系),当子类实例化对象的时候 会默认调用父类的无参构造方法,如果有 super()的话,调用的是父类有参的构造方法! 也就是说 父类必须有 有参构造 没有的话super ...
- zTree第三章,异步加载,前端
zTree异步加载 ---------------------------------------------------------------------------------- 具体详见API ...
- Linux 环境变量加强
Linux 环境变量加强 # 前言 今天,主要是之前搭建 GO 环境包的使用发现自己对 Linux 环境变量还不是很熟悉. 遇到环境变量的问题还是会有些懵逼.所以,今天写点Linux 环境变量的文章, ...
- mxonline实战-1,创建应用及相应模型
前言 环境说明:python3.5 + django2.0, 用的pycharm4.04专业版 课程视频地址 https://coding.imooc.com/learn/list/78. ...
- 部署LVS-DR群集
一.LVS-DR原理剖析 (一)LVS-DR数据包流向分析 1.Client向目标VIP发出请求,Director(负载均衡器)接收.此时IP包头及数据帧头信息为: 2.Director根据负载均衡算 ...
- Google 推出新搜索引擎以查找数据集
简评:谷歌推出了一个用于寻找数据集的新搜索引擎,有点厉害! 该工具可以更轻松地访问 Web 上数千个数据存储库中的数百万个数据集,当前还处于测试版: 什么是 Dataset Search? 数 ...
- CentOS7系统安装 Maria Db(MYSQL)教程
一.背景Maria Db是流行的跨平台MySQL数据库管理系统的分支,被认为是MySQL 的完全替代品.Maria Db是由Sun在Sun Micro systems合并期间被Oracle收购后,于2 ...