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)

我的答案如下:

 class Solution {
public: vector<string> restoreIpAddresses(string s) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int i=,j,k;
int len = s.size();
vector<string> result;
if (len> || len< )
return result;
for(i;i<len-;i++){
for(j=i+;j<len-;j++){
for(k=j+;k<len-;k++){
string s1 = s.substr(,i+);
string s2 = s.substr(i+,j-i);
string s3 = s.substr(j+,k-j);
string s4 = s.substr(k+);
if(isOK(s1) && isOK(s2) && isOK(s3) && isOK(s4)){
string s5 = s1 + "." + s2 + "." + s3 + "." + s4;
result.push_back(s5);
}
}
}
}
return result;
} bool isOK(string s){
int len = s.size();
if (len> || len <)
return false;
else if (==len)
return true;
else if (==len){
return (''!=s.at());
}else{//3==len
int a = (s.at()-'')* + (s.at()-'')*+(s.at()-'');
return (a>= && a<=);
}
}
};

我的答案

思路:遍历所有的可能性,因为最多也就是个长度为12的字符串,三层循环也不用考虑复杂度的问题。把parse出来的三个string检查一下是否为0至255之间的整数,如果四个都满足,那就是这个题目的一个解。主要首位数不能为0,例如03,012这样的不算是在0至255之间。

最新文章

  1. hibernate处理null 时提示:Property path [...] does notreference a collection
  2. javascript获取当前时间
  3. python3.4安装suds
  4. android 点击重新加载界面设计
  5. servlet简介
  6. leetcode算法刷题(二)——动态规划(一)
  7. Ajax请求URL后加随机数原理
  8. 推荐几个靠谱的VPN
  9. iOS 断点上传文件
  10. [国嵌笔记][006][Linux文本编辑器]
  11. ORACLE中修改表的Schema的总结
  12. Oracle技术面试问题
  13. noip2011 玛雅游戏 大模拟
  14. 一篇文章彻底搞懂es6 Promise
  15. Go-延时函数defer
  16. Unity3D|-XLua热更新用法的大致流程
  17. appium:运行脚本时,报404的解决办法
  18. 修改input 的 placeholder
  19. 本地Debug Asp.net MVC 无法加载css与js
  20. django403错误(转)

热门文章

  1. 【Linux】zlib安装
  2. KBMMW 4.84.00 发布
  3. vue组件实现查看大图效果
  4. 762. Prime Number of Set Bits in Binary Representation
  5. javascript捕获页面窗口关闭事件
  6. POSTMAN 数据关联
  7. 最全js表单验证
  8. c# 二维list排序和计时
  9. CodeForces 235E Number Challenge (莫比乌斯反演)
  10. sql计算经纬度得出最近距离的公式