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. bzoj2083: [Poi2010]Intelligence test(二分+vector)

    只是记录一下vector的用法 v.push_back(x)加入x v.pop_back()弹出最后一个元素 v[x]=v.back(),v.pop_back()删除x,但是会打乱vector顺序 v ...

  2. openssl安装相关软件

    出现:error: openssl/md5.h: No such file or directory 原因是libssl-dev 没有安装,执行: sudo apt-get install libss ...

  3. Uncaught (in promise) DOMException: The play() request was interrupted by a call to pause().

    解决方法: audio.load() let playPromise = audio.play() if (playPromise !== undefined) { playPromise.then( ...

  4. Efficient Deblurring for Shaken and Partially Saturated Images

    Try the online demo: http://willow-fd.rocq.inria.fr/unshake/ Overview One common feature of “shaken” ...

  5. bzoj 1568 [JSOI2008]Blue Mary开公司 超哥线段树

    [JSOI2008]Blue Mary开公司 Time Limit: 15 Sec  Memory Limit: 162 MBSubmit: 1808  Solved: 639[Submit][Sta ...

  6. java项目环境搭建

    开发java项目时,由于涉及到版权问题,最好使用开源.免费的软件.比如eclipse. 此外,一个web的java项目涉及到jdk.tomcat等,插件还可能用到svn插件.maven插件. 建议进入 ...

  7. [LeetCode] 6. ZigZag Conversion ☆☆☆

    The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...

  8. bzoj 1635: [Usaco2007 Jan]Tallest Cow 最高的牛——差分

    Description FJ's N (1 <= N <= 10,000) cows conveniently indexed 1..N are standing in a line. E ...

  9. 撩下Cookie和Session

    Cookie Cookie其实还可以用在一些方便用户的场景下,设想你某次登陆过一个网站,下次登录的时候不想再次输入账号了,怎么办?这个信息可以写到Cookie里面,访问网站的时候,网站页面的脚本可以读 ...

  10. margin 居中

    左右auto加个宽度.margin-left: auto; margin-right: auto; width:640px;