【一天一道LeetCode】#93. Restore IP Addresses
一天一道LeetCode
本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github
欢迎大家关注我的新浪微博,我的新浪微博
欢迎转载,转载请注明出处
(一)题目
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)
(二)解题
题目大意:给定一串字符,判断可以组成多少个有效的ip地址
解题过程中需要注意无效ip,诸如00,010等
解题思路:采用回溯法,ip有四级,分别判断每一级是否为有效的,依次递归,到最后判断能不能组成有效ip,没有就回溯到上一级继续递归。详细思路见代码注释
class Solution {
public:
vector<string> restoreIpAddresses(string s) {
vector<string> ret;
string ip;
dfsRestoreIp(s,ip,0,0,ret);
return ret;
}
void dfsRestoreIp(string& s , string ip , int count , int i ,vector<string>&ret)
{
if(count==4)//计数到4后表示一个ip地址
{
if(i==s.length()) {
ip.erase(ip.end()-1);//去除最后的'.'
ret.push_back(ip);
}
return;
}
int num = 0;
for(int j = i ; j < s.length() ; j++)
{
ip+=s[j];
num = num*10+(s[j]-'0');
if(num>0&&s[i]=='0') return;//去除诸如‘01’,‘010’等无效ip
if(num==0&&j-i>=1) return;//去除诸如'00'等无效ip
if(num<256)
{
ip+='.';//加上.代表到下一级
dfsRestoreIp(s,ip,count+1,j+1,ret);
ip.erase(ip.end()-1);//回溯到上一级
}
else return;
}
}
};
【一天一道LeetCode】#93. Restore IP Addresses的更多相关文章
- leetcode 93. Restore IP Addresses(DFS, 模拟)
题目链接 leetcode 93. Restore IP Addresses 题意 给定一段序列,判断可能组成ip数的所有可能集合 思路 可以采用模拟或者DFS的想法,把总的ip数分成四段,每段判断是 ...
- [LeetCode] 93. Restore IP Addresses 复原IP地址
Given a string containing only digits, restore it by returning all possible valid IP address combina ...
- LeetCode : 93. Restore IP Addresses
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABZ4AAAHUCAYAAAC6Zj2HAAAMFGlDQ1BJQ0MgUHJvZmlsZQAASImVlw
- leetcode 93 Restore IP Addresses ----- java
Given a string containing only digits, restore it by returning all possible valid IP address combina ...
- 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 ...
- 93. Restore IP Addresses
题目: Given a string containing only digits, restore it by returning all possible valid IP address com ...
- 【leetcode】Restore IP Addresses
Restore IP Addresses Given a string containing only digits, restore it by returning all possible val ...
- 【LeetCode】93. Restore IP Addresses 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 日期 题目地址:https://leetco ...
随机推荐
- Redis持久化的两种方式(RDB和AOF)
redis提供了两种持久化的方式,分别是RDB(Redis DataBase)和AOF(Append Only File). RDB,简而言之,就是在不同的时间点,将redis存储的数据生成快照并存储 ...
- Python小代码_9_求水仙花数
for i in range(100, 1000): ge = i % 10 shi = i // 10 % 10 bai = i // 100 if ge ** 3 + shi ** 3 + bai ...
- 360搜索引擎so自动收录php改写方案——适合phpcms等cms
360搜索引擎自动收录功能,官方提供了代码,带式,十分坑爹,没有提供批量提交入口,只是提供了一段js代码,关键是 一个js去下载另外一个js,document.write到文档,然后再 重复2遍如此工 ...
- ACM Least Common Multiple
The least common multiple (LCM) of a set of positive integers is the smallest positive integer which ...
- Go 语言切片(Slice)
Go 语言切片是对数组的抽象. Go 数组的长度不可改变,在特定场景中这样的集合就不太适用,Go中提供了一种灵活,功能强悍的内置类型切片("动态数组"),与数组相比切片的长度是不固 ...
- jQuery 遍历 – 过滤
缩小搜索元素的范围 三个最基本的过滤方法是:first(), last() 和 eq(),它们允许您基于其在一组元素中的位置来选择一个特定的元素. 其他过滤方法,比如 filter() 和 not() ...
- JavaScript Math(算数)对象
Math 对象 Math(算数)对象的作用是:执行普通的算数任务. Math 对象提供多种算数值类型和函数.无需在使用这个对象之前对它进行定义. 使用Math的属性/方法的语法: var x=Math ...
- Redis之(六)配置详解
进入Redis的安装包,里面的"redis.conf"就是默认的配置文件,启动Redis Server的时候,可以指定加载某个路径下的配置文件"redis-server ...
- Apache shiro集群实现 (六)分布式集群系统下的高可用session解决方案---Session共享
Apache shiro集群实现 (一) shiro入门介绍 Apache shiro集群实现 (二) shiro 的INI配置 Apache shiro集群实现 (三)shiro身份认证(Shiro ...
- 详解EBS接口开发之库存事务处理-物料批次导入
库存事务处理-物料批次导入 --系统批次表 SELECT * FROM MTL_LOT_NUMBERS T; --API创建批次 inv_lot_api_pub.create_inv_lot(x_re ...