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 ...
随机推荐
- can't add foreign key in mysql?
create table department (dept_name ), building ), budget numeric(,) ), primary key (dept_name) ); cr ...
- hdu 5652 India and China Origins 并查集+逆序
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5652 题意:一张n*m个格子的点,0表示可走,1表示堵塞.每个节点都是四方向走.开始输入初始状态方格, ...
- boost linux 下安装
1. 在boost 官网 http://www.boost.org/doc/libs/ 下载最新的boost 安装包 2. 解压至 /usr/local/ 目录下 3. cd /usr/local/b ...
- hadoop2.2.0+hive-0.10.0完全分布式安装方法
hadoop+hive-0.10.0完全分布式安装方法 1.jdk版本:jdk-7u60-linux-x64.tar.gz http://www.oracle.com/technetwork/cn/j ...
- componentsJoinedByString 和 componentsSeparatedByString 的方法的区别
将string字符串转换为array数组 NSArray *array = [Str componentsSeparatedByString:@","]; 将array数组转换为 ...
- DB天气app冲刺第七天
今天估计得分应该是最差的了. 今天因为完全没准备所以在界面UI上面根本就是相当于没弄.结果今天的各组报告里自己做得很不好.不过好在只是项目的中间期,不碍大事. 今天也完成了任务.可以说超额了也.因为吃 ...
- Python 学习日志(一)
第一天: (一)安装Python3.3: (二)试运行: 1.在IDLE中输入:print("Hello,world"); //回车查看结果 2.使用"File" ...
- configure脚本参数介绍
configure脚本有大量的命令行选项. 下面对每一个选项进行简略的介绍: --cache-file=FILE'configure' 会在你的系统上测试存在的特性(或者bug!).为了加速随后进行的 ...
- MongoDB实战指南(五):MongoDB中的聚集分析
聚集操作是对数据进行分析的有效手段.MongoDB主要提供了三种对数据进行分析计算的方式:管道模式聚集分析,MapReduce聚集分析,简单函数和命令的聚集分析. 1. 管道模式进行聚集 这里所说的管 ...
- javaweb学习总结(三十九)——数据库连接池
一.应用程序直接获取数据库连接的缺点 用户每次请求都需要向数据库获得链接,而数据库创建连接通常需要消耗相对较大的资源,创建时间也较长.假设网站一天10万访问量,数据库服务器就需要创建10万次连接,极大 ...