LeetCode:Restore IP Address
93. 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)
Tags: Backtracking String
输入一个字符串,要求输出该字符串可能拆分成的ip地址形式。
思路:ipv4的地址应该是4段,每段0-255中的一个数。所以考虑4层DFS加一些剪枝条件即可。剪枝条件有:1、取3个字符串stoi作为地址时大于255;2、2位数或者3位数地址段以0开头
另外要检查第四段ip地址为0的情况,防止类似于"0000000"atoi之后也算作0的情况。
class Solution {
public: bool dfs(string input, int number, string ipAddress, vector<string> &result){
if(input.length() == ){
return false;
}
if(number == ){
int addressNumber = stoi(input);
if(input[] == ''){
if(!(input.length() == && addressNumber == ))
return false;
}
if(addressNumber <= ){
ipAddress = ipAddress + input;
result.push_back(ipAddress);
return true;
}else{
return false;
}
}else{
if(input.length() >= ){
dfs(input.substr(), number + , ipAddress + input.substr(, ) + ".", result);
}
if(input.length() >= && input[] != ''){
dfs(input.substr(), number + , ipAddress + input.substr(, ) + ".", result);
}
if(input.length() >= && input[] != ''){
int addressNumber = stoi(input.substr(, ));
if(addressNumber <= ){
dfs(input.substr(), number + , ipAddress + input.substr(, ) + ".", result);
}
}
}
return true;
} vector<string> restoreIpAddresses(string s) {
vector<string> result;
if(s.length() == || s.length() > ){
return result;
}
string temp = "";
dfs(s, , temp, result);
return result;
}
};
LeetCode:Restore IP Address的更多相关文章
- [Leetcode] restore ip address 存储IP地址
Given a string containing only digits, restore it by returning all possible valid IP address combina ...
- [LeetCode] Restore IP Address [28]
题目 Given a string containing only digits, restore it by returning all possible valid IP address comb ...
- [LeetCode] Validate IP Address 验证IP地址
In this problem, your job to write a function to check whether a input string is a valid IPv4 addres ...
- [LeetCode] Restore IP Addresses 复原IP地址
Given a string containing only digits, restore it by returning all possible valid IP address combina ...
- [LintCode] Restore IP Address 复原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 解题报告
Restore IP Addresses My Submissions Question Solution Given a string containing only digits, restore ...
- [LeetCode] Restore IP Addresses 回溯
Given a string containing only digits, restore it by returning all possible valid IP address combina ...
随机推荐
- 一步步学习ASP.NET MVC3 (9)——JsonReslt,JavaScript,@Ajax
请注明转载地址:http://www.cnblogs.com/arhat 在上一章中,我们讲述了ActionResult的三个子类,非别是EmptyResult,RediretResult和Conte ...
- SDC(4)–set_clock_groups 与–add选项
1,set_clock_groups -exclusive 有多个时钟,但是多个时钟不会同时生效 例如: 2,-add 只有一个时钟输入源,但是始终的频率等可能变 例如:
- httpcontext in asp.net unit test
[TestMethod] [HostType("ASP.NET")] [UrlToTest("http://localhost:25153/qq/a.aspx" ...
- 创建共享内存函数CreateFileMapping()详解
测试创建和打开文件映射的时候老是得到"句柄无效"的错误, 仔细看了MSDN以后才发觉是函数认识不透, 这里把相关的解释翻译出来 HANDLE CreateFileMapping( ...
- 1842-A. Broj
#include <iostream> using namespace std; int main() { int n; cin>>n; if(n>0&& ...
- [jobdu]孩子们的游戏(圆圈中最后剩下的数)
这道题就是典型的约瑟夫环问题.http://blog.csdn.net/wuzhekai1985/article/details/6628491 一开始想了一下用数组来做,就是模拟方法,同时记录一下是 ...
- Android 发送HTTP GET POST 请求以及通过 MultipartEntityBuilder 上传文件
折腾了好几天的 HTTP 终于搞定了,经测试正常,不过是初步用例测试用的,因为后面还要修改先把当前版本保存在博客里吧. 其中POST因为涉及多段上传需要导入两个包文件,我用的是最新的 httpmine ...
- Android WebView 开发详解(一)
转载请注明出处 http://blog.csdn.net/typename/article/details/39030091 powered by meichal zhao 概览: Android ...
- 企业2.0杀出一号种子选手 “Linkwedo”横空出世
当下,最热门的话题就是企业2.0和1.0的新老交替,在过去的时间里OA在国内几乎是企业1.0的代名词,各大知名OA厂商一直占领着国内的企业市场,但企业2.0在全球越演越烈,甚至大有替代企业1.0的的迹 ...
- 如何使用MIME类型
今天在使用System.Net.WebClient做一个下载的时候,很郁闷,已经发不好的文件视频,却怎么也下载不了. 究其原因有两个, System.Net.WebClient对象的DownloadF ...