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. 简述JavaScript的类与对象

    JavaScript语言是动态类型的语言,基于对象并由事件驱动.用面向对象的思想来看,它也有类的概念.JavaScript 没有class关键字,就是用function来实现. 1. 实现方式及变量/ ...

  2. POJ3694:Network(并查集+缩点+lca)

    Network Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 13172   Accepted: 4774 题目链接:htt ...

  3. 题解 【luogu P1541 NOIp提高组2010 乌龟棋】

    题目链接 题解 题意: 有一些格子,每个格子有一定分数. 给你四种卡片,每次可以使用卡片来前进1或2或3或4个格子并拾取格子上的分数 每张卡片有数量限制.求最大分数. 分析 设\(dp[i]\)为第前 ...

  4. POJ 2391 二分+最大流

    Ombrophobic Bovines Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19066   Accepted: 4 ...

  5. 顺序统计:寻找序列中第k小的数

    最直观的解法,排序之后取下标为k的值即可. 但是此处采取的方法为类似快速排序分块的方法,利用一个支点将序列分为两个子序列(支点左边的值小于支点的值,支点右边大于等于支点的值). 如果支点下标等于k,则 ...

  6. last-child 选择器

    <!DOCTYPE html> <html> <head> <style> p:last-child //p的父类 的子类下最后一个,就是p兄弟层的最后 ...

  7. 爬虫服务集群处理nginx返回504

    最近在对爬虫服务做分布式服务的时候总是遇到服务器返回504,搞了两天才发现原来是nginx中有对超时的设置参数,自己都是用默认的,然而客户端的等待时间超过了nginx默认的超时设置 修改 keepal ...

  8. Maven命令行窗口指定settings.xml

    maven命令行窗口指定特定settings.xml 在命令行界面指定settings.xml,命令如下: mvn install --settings c:\user\settings.xml 例如 ...

  9. MongoDB入门(8)- c#通过操作MongoDB GridFS实现文件的数据库存储

    GridFS介绍 GridFS是MongoDB中的一个内置功能,可以用于存放大量小文件. GridFS GridFS长啥样 /* 1 */ { "_id" : ObjectId(& ...

  10. Spring实战第一部分总结

                                                         Spring实战第一部分总结 第一章 综述 1. DI依赖注入让相互协作的组件保持松散耦合,而 ...