Leetcode93. Restore IP Addresses复原IP地址
给定一个只包含数字的字符串,复原它并返回所有可能的 IP 地址格式。
示例:
输入: "25525511135" 输出: ["255.255.11.135", "255.255.111.35"]
题目很简单但是,需要特定判断的情况很多。
思路可以是递归和多重循环,。
因为该题没有太需要用到递归,所以直接用循环。
易错的地方会在代码中标注。
class Solution
{
public:
vector<string> restoreIpAddresses(string s)
{
vector<string> res;
int len = s.size();
///第一次
for (int i = 1; i <= 3; i++)
{
//判断是否超过了字符串的长度,除了最后一次,都要需要判断
if (i >= len)
continue;
string str1 = string(s.begin(), s.begin() + i);
//检查字符串是否符合要求
if (!Check(str1))
continue;
///第二次
for (int j = 1; j <= 3; j++)
{
if (i + j >= len)
continue;
string str2 = string(s.begin() + i, s.begin() + i + j);
if (!Check(str2))
continue;
///第三次和第四次
for (int k = 1; k <= 3; k++)
{
if (i + j + k >= len)
continue;
string str3 = string(s.begin() + i + j, s.begin() + i + j + k);
if (!Check(str3))
continue;
string str4 = string(s.begin() + i + j + k, s.end());
if (!Check(str4))
continue;
res.push_back(str1 + '.' + str2 + '.' + str3 + '.' + str4);
}
}
}
return res;
}
bool Check(string s)
{
//检测最后一个字符串str4是否为空
if (s.size() == 0)
return false;
//判断是否有011,01这种以0开头的字符串
if (s[0] == '0' && s.size() > 1)
return false;
int number = 0;
for (int i = 0; i < s.size(); i++)
{
number = number * 10 + (s[i] - '0');
//需要在里面进行判断是否超出范围
//因为number是字符串转来的,很可能超出了int的范围,超出后变成负数判断就比255小,返回true,显然这是不正确的
//所以在循环里面提前判断
if (number > 255)
return false;
}
return true;
}
};
Leetcode93. Restore IP Addresses复原IP地址的更多相关文章
- [LeetCode] Restore IP Addresses 复原IP地址
Given a string containing only digits, restore it by returning all possible valid IP address combina ...
- 093 Restore IP Addresses 复原IP地址
给定一个只包含数字的字符串,复原它并返回所有可能的IP地址格式.例如:给定 "25525511135",返回 ["255.255.11.135", " ...
- [LeetCode] 93. 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 ...
- 93. Restore IP Addresses
题目: Given a string containing only digits, restore it by returning all possible valid IP address com ...
- LeetCode解题报告—— Reverse Linked List II & Restore IP Addresses & Unique Binary Search Trees II
1. Reverse Linked List II Reverse a linked list from position m to n. Do it in-place and in one-pass ...
- leetcode -day29 Binary Tree Inorder Traversal & Restore IP Addresses
1. Binary Tree Inorder Traversal Given a binary tree, return the inorder traversal of its nodes' ...
- 【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
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
随机推荐
- NX二次开发-创建CSYS坐标系UF_CSYS_create_csys
NX9+VS2012 #include <uf.h> #include <uf_csys.h> #include <uf_mtx.h> UF_initialize( ...
- (转)RSA加密解密及数字签名Java实现
转:http://my.oschina.net/jiangli0502/blog/171263?fromerr=hc4izFe2 RSA公钥加密算法是1977年由罗纳德·李维斯特(Ron Rives ...
- nginx+keepalive 实现高可用负载均衡方案
转:http://ju.outofmemory.cn/entry/52165 主nginx负载均衡器:172.26.11.99 (通过keepalived配置了VIP:172.26.11.101供外 ...
- day 88 DjangoRestFramework学习二之序列化组件、视图组件
DjangoRestFramework学习二之序列化组件.视图组件 本节目录 一 序列化组件 二 视图组件 三 xxx 四 xxx 五 xxx 六 xxx 七 xxx 八 xxx 一 序列化组件 ...
- 如何在html中添加视频
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="445" heig ...
- Python3中string内置参数
说明: 使用ipython查看python3的内置函数 ,只需要输入字符串按两下tab键 capitalize():将字符串中第一个字符大写 casefold:将字符串中的所有大写字母转为小写 cen ...
- HDU—4046 Panda (线段树)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=4046 题意:给出一个字符串,统计这个字符串任意区间中"wbw"出现的次数. 规定两 ...
- Haar分类器方法
一.Haar分类器的前世今生 二.人脸检测属于计算机视觉的范畴,早期人们的主要研究方向是人脸识别,即根据人脸来识别人物的身份,后来在复杂背景下的人脸检测需求越来越大,人脸检测也逐渐作为一个单独的研究方 ...
- VS环境下,DEV插件的ComboBoxEdit控件最简单的数据源绑定和获取方法
使用 ComboBoxEdit 控件绑定key/value值: 因为 ComboBoxEdit 没有 DataSource 属性,所以不能直接绑定数据源,只能一项一项的添加. 首先创建一个类ListI ...
- chrome控制台使用jquery
html页面中加入:<script type="text/javascript" src="http://static.fanxian.com/script/jqu ...