给定一个只包含数字的字符串,复原它并返回所有可能的 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. NOIp2018集训test-9-21(am/pm)

    Am DAY1 抄代码 送分题 //Achen #include<bits/stdc++.h> #define For(i,a,b) for(int i=(a);i<=(b);i++ ...

  2. NX二次开发-NXOpen窗口打印NXMessageBox&ListingWindow

    NX9+VS2012 #include <NXOpen/NXObject.hxx> #include <NXOpen/Part.hxx> #include <NXOpen ...

  3. 微信小程序--跳转页面常用的两种方法

    一.bindtap="onProductsItemTap"绑定点击跳转事件 在.wxml文件中绑定 在.js文件中实现绑定事件函数 二.navigator标签配合URL跳转法 在w ...

  4. Gym101981D - 2018ACM-ICPC南京现场赛D题 Country Meow

    2018ACM-ICPC南京现场赛D题-Country Meow Problem D. Country Meow Input file: standard input Output file: sta ...

  5. RCC, Reset and Clock Control

  6. USACO 2003 Fall Orange Popular Cows /// tarjan缩点 oj22833

    题目大意: n头牛,m个崇拜关系,并且崇拜具有传递性 如果a崇拜b,b崇拜c,则a崇拜c 求最后有几头牛被所有牛崇拜 强连通分量内任意两点都能互达 所以只要强联通分量内有一点是 那么其它点也都会是 按 ...

  7. 字符串KMP算法

    讲解:http://blog.csdn.net/starstar1992/article/details/54913261 #include <bits/stdc++.h> using n ...

  8. ARM GNU 专有符号

    1. @ 表示注释从当前位置到行尾的字符. 2. # 注释掉一整行. 3. ; 新行分隔符.

  9. JS对象 向上取整ceil() ceil() 方法可对一个数进行向上取整。 语法: Math.ceil(x) 注意:它返回的是大于或等于x,并且与x最接近的整数。

    向上取整ceil() ceil() 方法可对一个数进行向上取整. 语法: Math.ceil(x) 参数说明: 注意:它返回的是大于或等于x,并且与x最接近的整数. 我们将把 ceil() 方法运用到 ...

  10. 整理下webapi的一些琐碎事情

    在使用webapi的时候我们会遇到一些问题比如 1.POST怎么请求 2.怎么兼容JSONP请求 3.怎么给指定端提供跨域的请求 4.怎么显示单独的models层的注释 问题一二其他人都玩的比较成熟的 ...