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的更多相关文章
随机推荐
- [CentOS]CentOS下编译CPP文件时报错[undefined reference to `__gxx_personality_v0' collect2: ld]的解决办法
在CentOS环境下编译CPP时报出 undefined reference to `__gxx_personality_v0' collect2: ld 以上错误,调查了一下,加上参数[-lstdc ...
- [转]解决Mysql InnoDB: Failing assertion: ret || !assert_on_error问题
国庆回来后,发现mysql停止服务了,没办法继续启动了.查看日志,看到: 131008 09:56:03 mysqld_safe Starting mysqld daemon with databas ...
- 使用python登录CNZZ访问量统计网站,然后获取相应的数据
思路: 第一步:使用pypeteer.launcher打开浏览器, 第二步:向CNZZ的登录(通过使用iframe嵌入的阿里巴巴单点登录页面),向iframe页面中自动输入用户名和密码,然后点击登录按 ...
- 10-08常用的TIME和DATE函数以及各个函数对应的头文件
系统时间和日期函数: #include <time.h> char *asctime(const struct tm *tm);//将tm中存放的信息转换为标准格式 ...
- maven的jar路径、下载路径
jar路径:在localRepository中填写先要放的位置 下载镜像:添加新的就可以 <mirror> <id>nexus-aliyun</id> <mi ...
- linux 使用内存作为 /tmp 临时文件夹
方法一:cat /etc/fstabtmpfs /tmp tmpfs defaults,noatime,mode=1777 0 0 方法二:mount tmpfs /tmp -t tmpfs -o s ...
- 【OCP 12c】最新CUUG OCP-071考试题库(62题)
62.(13-17)choose the best answer: You need to list the employees in DEPARTMENT_ID 30 in a single row ...
- lambda 、 map 、filter 、reduce 及 reversed 常用函数
lambda 匿名函数 什么是lambda? lambda 操作符(或 lambda 函数)通常用来创建小巧的,一次性的匿名函数对象.它的基本语法如下: lambda arguments : expr ...
- Ajax请求参数解释
ajax常用的两个请求是get和post,而springmvc的控制层可以接收ajax请求. 但是这个过程非常灵活,变化很大,容易出错. $.ajax({ url : SITE_PATH + &quo ...
- 高可用群集HA介绍与LVS+keepalived高可用群集
一.Keepalived介绍 通常使用keepalived技术配合LVS对director和存储进行双机热备,防止单点故障,keepalived专为LVS和HA设计的一款健康检查工具,但演变为后来不仅 ...