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)
这道题整了我很久,首先是一个老毛病犯了,又忘了区分字符'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——边界条件判定的更多相关文章
- 【leetcode】Restore IP Addresses
Restore IP Addresses Given a string containing only digits, restore it by returning all possible val ...
- 93. Restore IP Addresses
题目: Given a string containing only digits, restore it by returning all possible valid IP address com ...
- 93.Restore IP Addresses(M)
93.Restore IP Addresses Medium 617237FavoriteShare Given a string containing only digits, restore it ...
- 【LeetCode】93. Restore IP Addresses
Restore IP Addresses Given a string containing only digits, restore it by returning all possible val ...
- 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 22. Generate Parentheses Restore IP Addresses (*) 131. Palindrome Partitioning
backtracking and invariant during generating the parathese righjt > left (open bracket and cloas ...
- 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' ...
- leetcode 93. Restore IP Addresses(DFS, 模拟)
题目链接 leetcode 93. Restore IP Addresses 题意 给定一段序列,判断可能组成ip数的所有可能集合 思路 可以采用模拟或者DFS的想法,把总的ip数分成四段,每段判断是 ...
随机推荐
- scanf函数用法小记
By francis_hao Aug 26,2017 scanf – 输入格式转换 概述 #include <stdio.h>int scanf(const char *fo ...
- CursorAdapter中getView newView bindView异同
Adapter的作用是界面与数据之间的桥梁,通过设置适配器至ListView控件后(如调用ListView的 setAdapter(ListAdapter adapter) ...
- template.js的使用心得
template.js是一款JavaScript模板引擎,用来渲染页面的. 原理:提前将Html代码放进编写模板 <script id="tpl" type="te ...
- npm 淘宝镜像安装以及安装报错window_nt 6.1.7601 解决
http://www.cnblogs.com/ycxhandsome/p/6562980.html npm config set proxy null npm config set https-pro ...
- poj 2449 Remmarguts' Date (k短路模板)
Remmarguts' Date http://poj.org/problem?id=2449 Time Limit: 4000MS Memory Limit: 65536K Total Subm ...
- HDU 1203 I NEED A OFFER! (dp)
题目链接 Problem Description Speakless很早就想出国,现在他已经考完了所有需要的考试,准备了所有要准备的材料,于是,便需要去申请学校了.要申请国外的任何大学,你都要交纳一定 ...
- js_setCookie,getCookie和checkcookie函数
随便说说: cookie和sessionStrong,localStrong在web应用中都有一种存储的功能,也就是说可以把一些数据记录在浏览器.cookie和后两者的主要区别 是cookie是和后端 ...
- Java线程总结(一)
首先,先贴上一个简单的线程实例: public class MyThread extends Thread{ @Override public void run(){ try { for (int i ...
- js中的apply、call、bind
每个函数都包含两个非继承而来的方法,call()和apply(),可以改变函数内部this的指向 1.apply 用另一个对象替换当前对象,接收两个参数,第一个参数表示需要绑定的this变量,第二个参 ...
- Perl6 Bailador框架(6):获取用户输入
use v6; use Bailador; get '/' => sub { ' <html> <head><title></title>< ...