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 ...
随机推荐
- iOS 多层级的immutable objects 转换成 mutable objects
第一种方法是:将多层级的递归转换 方法: +(id) recursiveMutable:(id)object { if([object isKindOfClass:[NSDictionary clas ...
- 3.RabbitMQ 第一个程序
RabbitMQ消息服务器主要解决应用程序之间异步消息传输问题,传统的MQ分为点对点和主题与订阅,RabbitMQ使用Exchange(交换机)实现更加灵活的消息传递. 前面介绍过几个概念,Routi ...
- PAT_A1079#Total Sales of Supply Chain
Source: PAT A1079 Total Sales of Supply Chain (25 分) Description: A supply chain is a network of ret ...
- 顺时针打印矩阵元素(python实现)
我觉得我的算法比较简单易懂,比网上的那些简单些.至于时间复杂度就没有比较了. 算法思想:从最外层向内层遍历矩阵 # my algorithmimport math def print_matrix(m ...
- Spring 源码学习——注册 BeanDefinition
BeanFactory BeanFactory 是 Spring IoC 容器的具体实现,是 Spring 容器的核心接口. DefaultListableBeanFactory XmlBeanFac ...
- 解决maven项目创建过慢的问题以及快捷键
分别在创建项目时填入以下值: Name:archetypeCatalogValue:internal idea常用的快捷键 Alt+回车 导入包,自动修正 Ctrl+N 查找类 Ctrl+Shift+ ...
- VBA文件对话框的应用(VBA打开文件、VBA选择文件、VBA选择文件夹)
在VBA中经常要用到文件对话框来进行打开文件.选择文件或选择文件夹的操作.用Microsoft Office提供的文件对话框比较方便.用法如下Application.FileDialog(fileDi ...
- <a>标签中的href=""
href="javascript:;",其中javascript:是伪协议,它可以让我们通过一个链接来调用javascript函数.而采用这个方式 javascript:;可以实现 ...
- canvas填充规则,非零环绕
1.看一块区域是否填充 2.从这个区域拉一条直线 3,看和这条直线相交的轨迹 4.如果顺时针轨迹+1 5.如果逆时针轨迹-1 6.所有轨迹的值计算出来 7.如果是非0,那么填充 8.如果是0那么不填充
- leetcode-分治
题目169: 分治:O(nlgn) class Solution: def majorityElement(self, nums: List[int]) -> int: def majorE(l ...