[leetcode.com]算法题目 - Restore IP Addresses
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之间。
[leetcode.com]算法题目 - Restore IP Addresses的更多相关文章
- Leetcode 22. Generate Parentheses Restore IP Addresses (*) 131. Palindrome Partitioning
backtracking and invariant during generating the parathese righjt > left (open bracket and cloas ...
- LeetCode(93) Restore IP Addresses
题目 Given a string containing only digits, restore it by returning all possible valid IP address comb ...
- LeetCode之“字符串”:Restore IP Addresses
题目链接 题目要求: Given a string containing only digits, restore it by returning all possible valid IP addr ...
- leetcode 93. Restore IP Addresses(DFS, 模拟)
题目链接 leetcode 93. Restore IP Addresses 题意 给定一段序列,判断可能组成ip数的所有可能集合 思路 可以采用模拟或者DFS的想法,把总的ip数分成四段,每段判断是 ...
- LeetCode: Restore IP Addresses 解题报告
Restore IP Addresses My Submissions Question Solution Given a string containing only digits, restore ...
- LeetCode解题报告—— Reverse Linked List II & Restore IP Addresses & Unique Binary Search Trees II
1. Reverse Linked List II Reverse a linked list from position m to n. Do it in-place and in one-pass ...
- 【leetcode】Restore IP Addresses
Restore IP Addresses Given a string containing only digits, restore it by returning all possible val ...
- 【LeetCode】93. Restore IP Addresses
Restore IP Addresses Given a string containing only digits, restore it by returning all possible val ...
- leetcode -day29 Binary Tree Inorder Traversal & Restore IP Addresses
1. Binary Tree Inorder Traversal Given a binary tree, return the inorder traversal of its nodes' ...
随机推荐
- Flex 确定弹出窗口的绝对位置x , y
var selectbox:Selectbox; selectbox = new SelectBox(); var pt:Point = new Point(0,0); pt = this.paren ...
- 2018.12.19 codeforces 1092F. Tree with Maximum Cost(换根dp)
传送门 sbsbsb树形dpdpdp题. 题意简述:给出一棵边权为1的树,允许选任意一个点vvv为根,求∑i=1ndist(i,v)∗ai\sum_{i=1}^ndist(i,v)*a_i∑i=1n ...
- 2018.11.07 bzoj2751: [HAOI2012]容易题(easy)(组合数学)
传送门 组合数学一眼题. 感觉一直做这种题智商会降低. 利用组合数学的分步计数原理. 只用关心每个数不被限制的取值的总和然后乘起来就可以了. 对于大部分数都不会被限制,总和都是n(n+1)2\frac ...
- ACM-ICPC 2018 徐州赛区网络预赛 B BE, GE or NE(博弈,记忆化搜索)
链接https://nanti.jisuanke.com/t/31454 思路 开始没读懂题,也没注意看数据范围(1000*200的状态,记忆化搜索随便搞) 用记忆化搜索处理出来每个状态的胜负情况 因 ...
- c++关键字static的作用
1.隐藏 当同时编译多个源文件(.cpp)时,在某一个源文件中用static修饰的全局变量或函数,对其他源文件是隐藏的 //1.cpp ; void fun() { cout << < ...
- nullptr(c++11)
1.概念 用字面值常量nullptr来初始化或赋值来得到空指针 2.c++11之前使用NULL或0 1)NULL是一个宏定义(预处理变量),定义在cstdlib中,其值就是0:对于预处理变量,预处理器 ...
- 第18章:MongoDB-聚合操作--聚合管道--$sort
①$sort 使用“$sort”可以实现排序,设置1表示升序,设置-1表示降序. ②范例:实现排序
- 学以致用八---centos7.2 安装vim8+支持python3
目的:打造基于python的vim环境 也是在地铁上突然产生的一个想法,平时都是在pycharm上练习python,但有时候会提示激活码过期,又得上网找激活码,够折腾的.那何不在linux环境下来搭建 ...
- 批处理最完整人性化教程(.bat文件语法)
原文链接:http://www.cnitblog.com/seeyeah/archive/2009/01/15/53808.html 这是一篇技术教程,我会用很简单的文字表达清楚自己的意思,你要你识字 ...
- KindEditor4.x整合教程-Xproer.WordPaster
1.1. 集成到KindEditor4.x 主要步骤如下: 1.上传插件文件夹 2.上传WordPaster文件夹 3.在引用页面为KindEditor增加插件按钮 <html xmlns= ...