【一天一道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 ...
随机推荐
- 初识RabbitMQ系列之三:.net 如何使用RabbitMQ
话不多说,直接上代码! 一:搭建一个解决方案框架:RabbitMQ_Demo 其中包含4个部分: 1:RabbitMQ 公用类库项目 2:一个生产者控制台项目 3:两个消费者控制台项目 项目结构如图: ...
- webstorm2017破解
选择"license server" 输入:http://idea.imsxm.com/ 2017支持vue了
- sublime下配置C/C++运行环境
最近在学习<WEB前端课程>老师教我们使用DW,但是不太喜欢,就选择了sublime,写前端代码还是很方便. 平时都是写C++,C比较多,借鉴了别人的配置步骤,将sublime打造成IDE ...
- Bootstrap 遮罩层实现方式
直接上代码: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <me ...
- UI相关
前端 UI 框架 https://github.com/twbs/bootstrap https://github.com/google/material-design-lite https://gi ...
- MongoDB 复制(副本集)
MongoDB复制是将数据同步在多个服务器的过程. 复制提供了数据的冗余备份,并在多个服务器上存储数据副本,提高了数据的可用性, 并可以保证数据的安全性. 复制还允许您从硬件故障和服务中断中恢复数据. ...
- chrome官方完整安装包
But did you know Google allows you to download the full standalone installer of Chrome from its offi ...
- Java中获取文件大小的正确方法
本文出处:http://blog.csdn.net/djy1992/article/details/51146837,转载请注明.由于本人不定期会整理相关博文,会对相应内容作出完善.因此强烈建议在原始 ...
- 算法之路(三)----查找斐波纳契数列中第 N 个数
算法题目 查找斐波纳契数列中第 N 个数. 所谓的斐波纳契数列是指: * 前2个数是 0 和 1 . * 第 i 个数是第 i-1 个数和第i-2 个数的和. 斐波纳契数列的前10个数字是: 0, 1 ...
- RxJava操作符(09-算术/聚合操作&连接操作)
转载请标明出处: http://blog.csdn.net/xmxkf/article/details/51692493 本文出自:[openXu的博客] 目录: 算术聚合 Count Concat ...