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的更多相关文章

随机推荐

  1. 基于S2SH开发病房管理系统的设计与实现 源码

    基于S2SH开发病房管理系统的设计与实现: 开发环境: Windows操作系统 开发工具:Eclipse/MyEclipse+Jdk+Tomcat+MySQL数据库 运行效果图:       此源码经 ...

  2. redis分布式集群3种架构方案

    集群方案: 1. 主从高可用(该方案就是单实例形式,只是为了保证数据的安全,对于用户数据少,业务的前期可以采用,目前我司缓存架构就是采用该方案) 2. 客户端分片(典型代表:Jedis.自主写分片算法 ...

  3. PHP爬虫(3)PHP DOM开源代码里的大坑和字符编码

    一.开源代码的问题 在PHP爬虫(2)中介绍了开源工程Sunra.PhpSimple.HtmlDomParser.在实际工作中发现一个问题,例如http://www.163.com的网页数据怎么也抓取 ...

  4. 分布式流式计算平台——S4

    本文是作者在充分阅读和理解Yahoo!最新发布的技术论文<S4:Distributed Stream Computing Platform>的基础上,所做出的知识分享. S4是Yahoo! ...

  5. asp.net mvc部分视图的action中获取父级视图信息

    RouteData.DataTokens["ParentActionViewContext"]中包含了父级视图的相关信息,如路由等 public ActionResult Chil ...

  6. PostSharp 结合 log4net 自动记录日志

    环境: VS 2012 PostSharp-4.1.28  (下载地址)https://visualstudiogallery.msdn.microsoft.com/a058d5d3-e654-43f ...

  7. unity 移动物体的方式

    1. 简介 在Unity3D中,有多种方式可以改变物体的坐标,实现移动的目的,其本质是每帧修改物体的position. 2. 通过Transform组件移动物体 Transform 组件用于描述物体在 ...

  8. 使用wblockCloneObjects从后台读取dwg文件复制实体到当前数据库

    AcDbDatabase *pNewDb=new AcDbDatabase(Adesk::kFalse); if (pNewDb == NULL) { return; } Acad::ErrorSta ...

  9. ie 8及以下 前端cors ajax跨域须知

    http://www.cnblogs.com/xishuai/p/jquery-ajax-ie8-cors.html

  10. Laravel 完整开源项目大全

    来自 Laravel学院:http://laravelacademy.org/ http://laravelacademy.org/laravel-project 原型项目 Laravel 5 Boi ...