LeetCode Restore IP Addresses
DFS
class Solution {
public:
vector<string> restoreIpAddresses(string s)
{
return insertDot(s, 0, 3);
} vector<string> insertDot(string s, int beginIndex, int countOfDot /*3, 2, 1, 0*/)
{
vector<string> result;
if( (s.size() - beginIndex) < (countOfDot + 1) || (s.size() - beginIndex - 1) > (countOfDot +1) * 3)
return result; if(countOfDot == 0)
{
string partition = s.substr(beginIndex);
if(partition.size() > 1 && partition[0] == '0') //Error 4: if the first char is 0, then no more chars, such as 1.00.1.1 is no valid;
{
return result;
}
int val = std::stoi(partition);
if(val >= 0 && val <256)
{
result.push_back(partition);
}
return result;
} for(int i = beginIndex + 1; i< s.size();i++ ) //Error 1: i< s.size() -1
{
string partition = s.substr(beginIndex, i - beginIndex);
if(partition.size() > 1 && partition[0] == '0') //Error 3: if the first char is 0, then no more chars, such as 1.00.1.1 is no valid;
{
break;
}
int val = std::stoi(partition);
if(val > 255)
break;
if(val >= 0 && val <256)
{
vector<string> subresult = insertDot(s, i, countOfDot - 1);
if(subresult.size() != 0)
{
for(int j = 0; j< subresult.size(); j++) //Error 2: j< s.size() -1
{
string onepartition = partition + "." + subresult[j];
result.push_back(onepartition);
}
}
}
}
return result;
}
};
LeetCode Restore IP Addresses的更多相关文章
- LeetCode: Restore IP Addresses 解题报告
Restore IP Addresses My Submissions Question Solution Given a string containing only digits, restore ...
- [LeetCode] Restore IP Addresses 复原IP地址
Given a string containing only digits, restore it by returning all possible valid IP address combina ...
- [leetcode]Restore IP Addresses @ Python
原题地址:https://oj.leetcode.com/problems/restore-ip-addresses/ 题意: Given a string containing only digit ...
- LeetCode——Restore IP Addresses
Given a string containing only digits, restore it by returning all possible valid IP address combina ...
- [LeetCode] Restore IP Addresses 回溯
Given a string containing only digits, restore it by returning all possible valid IP address combina ...
- leetcode 93. Restore IP Addresses(DFS, 模拟)
题目链接 leetcode 93. Restore IP Addresses 题意 给定一段序列,判断可能组成ip数的所有可能集合 思路 可以采用模拟或者DFS的想法,把总的ip数分成四段,每段判断是 ...
- 【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解题报告—— 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 ...
随机推荐
- ireport5.6+jasperreport6.3开发(三)--以javabean为基准的报表开发(javabean)
这里只有ireport的开发没有web侧的程序. ireport的数据源可以说是多种多样,大致可以通过文件 数据库 bean类这三种方式,这里只介绍bean类 (数据库比较简单可参考其他的网站,文件没 ...
- B-树,B+树,B*树详解
B-树 B-树是一种多路搜索树(并不一定是二叉的) 1970年,R.Bayer和E.mccreight提出了一种适用于外查找的树,它是一种平衡的多叉树,称为B树(或B-树.B_树). 一棵m阶B树(b ...
- CSS样式应用
CSS样式应用的方法: (1)行内样式,将css样式直接放到标签当中,一般都是放入标签的style属性中,它是最方便的一种样式,也是最不方便修改的样式.如下: (2)内嵌式,通过将css写在网页源文件 ...
- Android studio下gradle Robolectric单元测试配置
android studio下gradle Robolectric单元测试配置 1.Robolectric Robolectric是一个基于junit之上的单元测试框架.它并不依赖于Android提供 ...
- jquery.fullPage.js全屏滚动插件教程演示
css部分(此处需要导入jquery.fullPage.css) <style> .section { text-align: center; font: 50px "Micro ...
- PHP弱类型需要特别注意的问题
下面介绍的问题都已验证, 总结:字符数据比较==不比较类型,会将字符转数据,字符转数字(转换直到遇到一个非数字的字符.即使出现无法转换的字符串,intval()不会报错而是返回0).0e,0x开头的字 ...
- Java代理模式/静态代理/动态代理
代理模式:即Proxy Pattern,常用的设计模式之一.代理模式的主要作用是为其他对象提供一种代理以控制对这个对象的访问. 代理概念 :为某个对象提供一个代理,以控制对这个对象的访问. 代理类和委 ...
- Tableau10.0学习随记-分组问题
1.根据官网的练习视频,分组时可多选列,之后使用回形针按钮创建分组,并重新命名即可,截图如下: 2.但在Tableau10中打开练习工作簿练习时,并没有直接显示分组后结果,仅仅是创建了分组的纬度,结果 ...
- BZOJ1828 [Usaco2010 Mar]balloc 农场分配
直接贪心,我们把线段按照右端点从小到大排序,然后一个个尝试插入即可... 来证明贪心的正确性: 不妨设贪心得到的答案集合为$S$,最优解的答案集合为$T$ 若$S$不是最优解,那么$S \not= T ...
- 【转】PostgreSQL分布式事务配置
XA是open group提出的分布式事务处理规范,JTA支持XA规范,JTA只规定了接口,有些应用容器提供实现,也有一些三方的开源实现可用,比如Atomikos. 如果PostgreSQL参与分布式 ...