leetcode93 Restore IP Addresses
2016-01-06 16:09
260 查看
Given a string containing only digits, restore it by returning all possible valid IP address combinations.
For example:
Given
return
View Code
For example:
Given
"25525511135",
return
["255.255.11.135", "255.255.111.35"]. (Order does not matter)
class Solution { public: vector<string> restoreIpAddresses(string s) { vector<string> ans; vector<int> cur(8,0); dep(ans,s,0,cur,0); return ans; } void dep(vector<string> &ans,string s,int ps,vector<int>&cur,int pc) { int len=s.length(); for(int i=1;i<=3;i++) { if(ps+i<=len) { if(judge(s.substr(ps,i))) { cur[pc]=ps; cur[pc+1]=i; if(ps+i==len&&pc==6) addstr(ans,s,cur); else if(ps+i!=len&&pc!=6) dep(ans,s,ps+i,cur,pc+2); } } } } bool judge(string str) { if(str.length()>1&&str[0]=='0') return false; int temp=atoi(str.c_str()); if(temp>=0&&temp<=255) return true; return false; } void addstr(vector<string> &ans,string s,vector<int>&cur) { string temp=""; temp+=s.substr(cur[0],cur[1]); temp+="."; temp+=s.substr(cur[2],cur[3]); temp+="."; temp+=s.substr(cur[4],cur[5]); temp+="."; temp+=s.substr(cur[6],cur[7]); ans.push_back(temp); } };
View Code
相关文章推荐
- leetcode92 Reverse Linked List II
- Java常用工具类(一)
- Java单例模式探讨
- 每天写一点
- Unity3D中Animation的常见属性及方法
- Machine Learning-感知器学习算法
- muSowT.1
- linux命令
- Egret)连续tween的实现
- Eclipse中CVS界面功能描述
- [Magento代码]-导入excel添加分类,并关联产品到该分类下
- CMD规范的函数与普通函数间调用
- 最容易理解的HMM文章
- [bigdata] kafka基本命令 -- 迁移topic partition到指定的broker
- 网站集成第三方登录--Weinx
- WindowManager: android.view.WindowLeaked: Activity has leaked window com.android.internal.policy.imp
- 学习鸟哥的Linux私房菜笔记(5)——目录
- eclipse 安装 Fatjar 报错Failed to prepare partial IU
- Egret)tween的onChange事件写法
- JSP相关的内容