LeetCode93. 复原IP地址
93. 复原IP地址
给定一个只包含数字的字符串,复原它并返回所有可能的 IP 地址格式。
输入"010010"
输出["0.10.0.10","0.100.1.0"]
思路
①字符串长度为[4,12]符合条件,然后将小数点遍历,找出p1,p2,p3,p4的值[0,255]符合条件,p1举例:当开头的字符串是0的时候,字符串长度只能为0.当p1长度大于3则返回false(>255)。
class Solution {
private:bool judge(string s, int a, int b, int c, int d, int len){if(b-a>3 || c-b>3 || d-c>3 || len-d>3) return false;if((s[a]=='0' && b-a!=1) ||(s[b]=='0' && c-b!=1) ||(s[c]=='0' && d-c!=1) ||(s[d]=='0' && len-d!=1)) return false;int p1=0,p2=0,p3=0,p4=0;p1 = atoi(s.substr(a,b-a).c_str());p2 = atoi(s.substr(b,c-b).c_str());p3 = atoi(s.substr(c,d-c).c_str());p4 = atoi(s.substr(d,len-d).c_str());if(p1>255 || p2>255 || p3>255 ||p4>255) return false;string one = to_string(p1)+'.'+to_string(p2)+'.'+to_string(p3)+'.'+to_string(p4);bool flag=false;for(int i=0;i<res.size();i++){if(res[i]==one){flag=true;break;}}if(!flag)res.push_back(one);return true;}
public:vector<string> res;vector<string> restoreIpAddresses(string s) {res.clear();//清空int len = s.length();if(len>12 || len<4) return res;for(int b=1;b<len;b++){for(int c=b+1;c<len;c++){for(int d=c+1;d<len;d++){judge(s,0,b,c,d,len);}}}return res;}
};
LeetCode93. 复原IP地址相关推荐
- leetcode93. 复原IP地址(回溯)
给定一个只包含数字的字符串,复原它并返回所有可能的 IP 地址格式. 有效的 IP 地址正好由四个整数(每个整数位于 0 到 255 之间组成),整数之间用 '.' 分隔. 示例: 输入: " ...
- [Swift]LeetCode93. 复原IP地址 | Restore IP Addresses
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ ➤微信公众号:山青咏芝(shanqingyongzhi) ➤博客园地址:山青咏芝(https://www.cnblog ...
- leetcode93. 复原 IP 地址
一:每日论语解读 follow me !! 君子坦荡荡 小人常威威 二:题目 三:上码 class Solution {public:/**思路:1.分析题意本题依然是切割字符串(我们需要对要分割的字 ...
- 2022-5-15 Leetcode93.复原IP地址
要注意各种细节 class Solution {private:vector<string> ans; public:bool isValid(const string& s, i ...
- leetcode93. 复原 IP 地址(思路+详解)
一:题目 二:上码 class Solution {/**思路: 1.跟分割字符串差不多,但是我们需要记录添加的'.'的个数 当其个数等于3的时候 我们就已经切割成4段了.2.判断字符是否合法1> ...
- [LeetCode] Restore IP Addresses 复原IP地址
Given a string containing only digits, restore it by returning all possible valid IP address combina ...
- [LeetCode] Restore IP Addresses 复原IP地址
Given a string containing only digits, restore it by returning all possible valid IP address combina ...
- java实现复原IP地址,回溯算法:复原IP地址
93.复原IP地址 题目地址:https://leetcode-cn.com/problems/restore-ip-addresses/ 给定一个只包含数字的字符串,复原它并返回所有可能的 IP 地 ...
- 回溯算法——复原IP地址(Leetcode 93)
题目选自Leetcode 93.复原IP地址 由于我们需要找出所有可能复原出的 IP 地址,因此可以考虑使用回溯的方法,对所有可能的字符串分隔方式进行搜索,并筛选出满足要求的作为答案. 通俗来讲,就是 ...
最新文章
- mysql忘记密码,如何重置密码
- 基于快速原型模型建立商业呼叫中心SPOMP的应用研究
- java程序设计 第2版 唐大仕_《Java程序设计(第2版)》唐大仕 源代码
- SAP BI工具的优缺点
- 用Socket 打造跨语言跨操作系统的网络MORPG游戏(三)
- 【Leetcode】【Longest Palindromic Substring】【最长回文子串】【C++】
- 【渝粤教育】国家开放大学2018年春季 8625-21T老年心理健康 参考试题
- 【转】百度站长平台MIP引入工具使用心得
- android关机背景,鍵盤消失后的Android白色背景
- java执行class找不到main函数_你所不知道的HelloWorld背后的执行原理
- Python中的无序集合(set)
- (转)水晶报表横向打印BUG处理
- PuTTYgen使用教程
- 【卡尔曼滤波原理及基本认知】
- 细粒度分类:Hierarchical Bilinear Pooling(HBP),分级双线性池化(一)
- springboot:运行(部署)时出现WebServerException: Unable to create tempDir.
- 流利阅读 2019.3.18 Can baijiu, China’s sorghum firewater, go global?
- TCP四次挥手及原因
- 两点告诉您品牌控价(管控低价乱价)的重要性
- jQuery---动画
热门文章
- 大数据_Hbase_面试题0001
- tensorflow 初认识
- delphi 调用Msftedit.dll,重写Richedit,支持RTF画表格
- linux 中文编码环境,linux中文编码问题总结
- android 布局 不同屏幕大小,如何创建支持不同屏幕尺寸的Android应用
- mysql char 二进制_SQL:char 和 varchar、binary 和 varbinary、二进制字符串、严格模式、汉字编码方式...
- android studio自定义类,为AndroidStudio设置自定义类注释
- c语言三级试题及答案,2013年计算机二级C语言模拟试题五及答案
- v-show组件 vue_Vue.js教程--基础(实例 模版语法template computed, watch v-if, v-show v-for, 一个组件的v-for.)...
- Vue-CLI and Leaflet (9): 点聚合 Leaflet.markercluster