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)

这道题整了我很久,首先是一个老毛病犯了,又忘了区分字符'0'和数字0了,导致判断的时候出错,还有,最后结果的push

res.push_back( temp.substr(0,temp.size()-1));这样是对的,刚才出错是因为,temp=temp.substr(0,temp.size()-1);res.push_back(temp),这样子回溯的时候,会多删除一个。思路没错,就是因为这两个细节的错误导致这个程序调了很久。

整体思路就是深度优先搜索,首先看到边界条件没,如果没有,就深度搜索:

一开始搜索1个字符和剩下的字符串,判断该字符的index是否越界了,对于剩下的字符串递归;

然后是2个字符和剩下的字符串,判断这2个字符的首字符是否是0,对于剩下的字符串递归;

然后是3个字符和剩下的字符串,判断这3个字符的首字符是否是0,并且这3个字符组成的数字是否小于等于255,对于剩下的字符串递归。

class Solution {
private:
vector<string> res;
string temp;
int lenth;
public:
bool isValid(string subs)
{ if(subs[]==''&&subs.size()!=)
return false;
int num;
stringstream ss(subs);
ss >> num;
ss.clear();
if(num>)
return false;
else
return true;
}
void TestRest(string rests,int count)
{
if(count==)
{
if(rests.size()==)
{
res.push_back( temp.substr(,temp.size()-));
return ;
}
else
return;
}
else
{
if(rests.size()==)
return ;
}
string flag;
for(int i=;i<;i++)
{
if(i>=rests.size())
break;
flag.push_back(rests[i]);
if(isValid(flag))
{
temp+=flag+".";
TestRest(rests.substr(i+),count+);
temp=temp.substr(,temp.size()-flag.size()-);
}
else
break;
}
return ; }
vector<string> restoreIpAddresses(string s) {
string flag;
lenth=s.size();
if(lenth>||lenth<)
return res;
for(int i=;i<;i++)
{
flag.push_back(s[i]);
if(isValid(flag))
{
temp+=flag+".";
TestRest(s.substr(i+),);
temp=temp.substr(,temp.size()-flag.size()-);
}
else
break;
}
return res;
}
};

Restore IP Addresses——边界条件判定的更多相关文章

  1. 【leetcode】Restore IP Addresses

    Restore IP Addresses Given a string containing only digits, restore it by returning all possible val ...

  2. 93. Restore IP Addresses

    题目: Given a string containing only digits, restore it by returning all possible valid IP address com ...

  3. 93.Restore IP Addresses(M)

    93.Restore IP Addresses Medium 617237FavoriteShare Given a string containing only digits, restore it ...

  4. 【LeetCode】93. Restore IP Addresses

    Restore IP Addresses Given a string containing only digits, restore it by returning all possible val ...

  5. LeetCode: Restore IP Addresses 解题报告

    Restore IP Addresses My Submissions Question Solution Given a string containing only digits, restore ...

  6. 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 ...

  7. Leetcode 22. Generate Parentheses Restore IP Addresses (*) 131. Palindrome Partitioning

    backtracking and invariant during generating the parathese righjt > left  (open bracket and cloas ...

  8. leetcode -day29 Binary Tree Inorder Traversal &amp; Restore IP Addresses

    1.  Binary Tree Inorder Traversal Given a binary tree, return the inorder traversal of its nodes' ...

  9. leetcode 93. Restore IP Addresses(DFS, 模拟)

    题目链接 leetcode 93. Restore IP Addresses 题意 给定一段序列,判断可能组成ip数的所有可能集合 思路 可以采用模拟或者DFS的想法,把总的ip数分成四段,每段判断是 ...

随机推荐

  1. Codeforces Round #331 (Div. 2) A

    A. Wilbur and Swimming Pool time limit per test 1 second memory limit per test 256 megabytes input s ...

  2. Star sky 二维前缀和

    C. Star sky time limit per test 2 seconds memory limit per test 256 megabytes input standard input o ...

  3. 手脱ACProtect V1.4X(有Stolen Code)之补区段

    首先需要说的是,这个壳是ximo大神视频教程里的 0041F000 > pushad ; //程序入口点 0041F001 E8 call NgaMy.0041F007 0041F006 E8 ...

  4. Qt ------ 内存回收机制、new对象的回收

    写在前面的总结: 建议:对于不能指定父对象的对象(对象通过moveToThread()移入其他线程.没有继承QObject的类产生的对象),在其他线程通过deleteLater()内存回收,其他通过指 ...

  5. arm-linux-gcc等交叉编译工具的安装

    1.软件安装 步骤1:打开虚拟机,在/usr/local/下创建/usr/local/arm文件夹(一般用户自定义程序放到这里) 步骤2:先将安装包从Windows中弄到linux中去.可以用共享文件 ...

  6. Java 异常(Java Exception)

    Java异常    异常指不期而至的各种状况,如:文件找不到.网络连接失败.非法参数等.异常是一个事件,它发生在程序运行期间,干扰了正常的指令流程.Java通 过API中Throwable类的众多子类 ...

  7. 「6月雅礼集训 2017 Day8」gcd

    [题目大意] 定义times(a, b)表示用辗转相除计算a和b的最大公约数所需步骤. 那么有: 1. times(a, b) = times(b, a) 2. times(a, 0) = 0 3. ...

  8. Spring理论基础-控制反转和依赖注入

    第一次了解到控制反转(Inversion of Control)这个概念,是在学习Spring框架的时候.IOC和AOP作为Spring的两大特征,自然是要去好好学学的.而依赖注入(Dependenc ...

  9. python数据处理课程笔记(一)

    一.numpy 1.numpy中所有元素必须是相同的类型 a=[1,2,3,4,'t'] #列表中有str类型,转换为ndarray时所有元素都转换为str类型 arr1=np.array(a) pr ...

  10. bzoj 1004 burnside 引理+DP

    对于burnside引理需要枚举染色,这道题属于burnside的一种简单求解的方法,就是polya,我们可以使每一种置换中的循环节中的元素的颜色都相同,那么这样的话就可以直接DP了,我们可以将m个置 ...