给定一个只包含数字的字符串,复原它并返回所有可能的 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地址的更多相关文章

  1. [LeetCode] Restore IP Addresses 复原IP地址

    Given a string containing only digits, restore it by returning all possible valid IP address combina ...

  2. 093 Restore IP Addresses 复原IP地址

    给定一个只包含数字的字符串,复原它并返回所有可能的IP地址格式.例如:给定 "25525511135",返回 ["255.255.11.135", " ...

  3. [LeetCode] 93. Restore IP Addresses 复原IP地址

    Given a string containing only digits, restore it by returning all possible valid IP address combina ...

  4. [LintCode] Restore IP Address 复原IP地址

    Given a string containing only digits, restore it by returning all possible valid IP address combina ...

  5. 93. Restore IP Addresses

    题目: Given a string containing only digits, restore it by returning all possible valid IP address com ...

  6. 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 ...

  7. leetcode -day29 Binary Tree Inorder Traversal &amp; Restore IP Addresses

    1.  Binary Tree Inorder Traversal Given a binary tree, return the inorder traversal of its nodes' ...

  8. 【leetcode】Restore IP Addresses

    Restore IP Addresses Given a string containing only digits, restore it by returning all possible val ...

  9. 【一天一道LeetCode】#93. Restore IP Addresses

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

随机推荐

  1. NX二次开发-遍历当前part所有component,把装配子部件设置成工作部件

    NX11+VS2013 #include <uf.h> #include <uf_disp.h> #include <uf_modl.h> #include < ...

  2. [JZOJ 5791] 阶乘

    题意:求一个最小的\(m\),保证\(\prod a[i] * x = m!\) 思路: 考虑\(m!\)里面有多少个东西?? \(m\)个. 且是一个排列. 那么求一个最小的\(m\)使得前面的式子 ...

  3. BZOJ随即跳题-随即到什么题你写什么题

    来挑战一下吧~ 请事先登录你BZOJ的账号!

  4. pycharm 参数、快捷键、调试模式

    PyCharm参数.快捷键.调试模式 PyCharm设置参数 在运行Python脚本时,会经常遇到需要传入额外的参数来运行脚本. 例如下脚本1: #!/usr/bin/env python2 # *. ...

  5. How to Add Swap on CentOS

    About Linux Swapping Linux RAM is composed of chunks of memory called pages. To free up pages of RAM ...

  6. 洛谷P1860——新魔法药水

    传送门:QAQQAQ 题意:商店里有N种药水,每种药水都有一个售价和回收价.小S攒了V元钱,还会M种魔法,可以把一些药水合成另一种药水.他一天可以使用K次魔法,问他一天最多赚多少钱? N<=60 ...

  7. 专题:“find -perm”

    Search for files which have read and write permission for their owner, and group, but which other us ...

  8. Map获取键值,Map的几种遍历方法 (转载)

    Map类提供了一个称为entrySet()的方法,这个方法返回一个Map.Entry实例化后的对象集.接着,Map.Entry类提供了一个getKey()方法和一个getValue()方法,Map.E ...

  9. 使用neo4j图数据库的import工具导入数据 -方法和注意事项

    背景 最近我在尝试存储知识图谱的过程中,接触到了Neo4j图数据库,这里我摘取了一段Neo4j的简介: Neo4j是一个高性能的,NOSQL图形数据库,它将结构化数据存储在网络上而不是表中.它是一个嵌 ...

  10. Flutter BottomNavigationBar切换会刷新当前页面解决方

    问题描述 BottomNavigationBar 是flutter 中最常用的UI组建,刚接触时发现在切换tab 的时候,会刷新当前的所有状态,每个页面都会重新刷新.于是乎,在这里先记录下解决方案. ...