题目:

Given a string containing only digits, restore it by returning all possible valid IP address combinations. (Medium)

For example:
Given "25525511135",

return ["255.255.11.135", "255.255.111.35"]. (Order does not matter)

分析:

使用回溯法即可。helper函数用来处理DFS过程,isValid判断一个子串是否符合IP地址一个点分十进制的一块。

注意在isValid中要除去类似00,01这种0打头的情况。

代码:

 class Solution {
private:
vector<string> result;
bool isValid(const string& s) {
if (s[] == '' && s.size() > ) { // for case like "00.122.122.122"
return false;
}
int num = stoi(s);
if (num >= && num <= ) {
return true;
}
return false;
}
void helper(const string& s, string curS, int start, int step) {
if (step == ) {
if (start != s.size()) {
return;
}
curS.pop_back();
result.push_back(curS);
return;
}
for (int i = ; i <= ; ++i) {
if (start + i <= s.size()) {
string tempS = s.substr(start, i);
if (isValid(tempS)) {
string newS = curS + tempS;
newS.push_back('.');
helper(s, newS, start + i, step + );
}
}
}
}
public:
vector<string> restoreIpAddresses(string s) {
string curS;
helper(s, curS, , );
return result;
}
};

LeetCode93 Restore IP Addresses的更多相关文章

  1. Leetcode93. Restore IP Addresses复原IP地址

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

  2. 【leetcode】Restore IP Addresses

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

  3. 93. Restore IP Addresses

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

  4. 93.Restore IP Addresses(M)

    93.Restore IP Addresses Medium 617237FavoriteShare Given a string containing only digits, restore it ...

  5. 【LeetCode】93. Restore IP Addresses

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

  6. LeetCode: Restore IP Addresses 解题报告

    Restore IP Addresses My Submissions Question Solution Given a string containing only digits, restore ...

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

  8. Leetcode 22. Generate Parentheses Restore IP Addresses (*) 131. Palindrome Partitioning

    backtracking and invariant during generating the parathese righjt > left  (open bracket and cloas ...

  9. 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' ...

随机推荐

  1. [原创]最优化/Optimization文章合集

    转载请注明出处:https://www.codelast.com/ 最优化(Optimization)是应用数学的一个分支,它是研究在给定约束之下如何寻求某些因素(的量),以使某一(或某些)指标达到最 ...

  2. PHP jpgraph的一点小提示和方法

    PHP默认是不启用GD库的,因为需要在php.ini的配置文件中将extension=php_gd2.dll注释打开.打开后你就可以画一些你想画的各种奇葩图案了.什么?不会画?那回去学基础! 今天看了 ...

  3. java反编译工具使用记录

    最近试了四个反编译工具,总结一下. 先说结论,最有效果的是Procyon Decompile.使用方法:https://blog.csdn.net/u010762551/article/details ...

  4. Ubuntu中安装gdal python版本

    安装过程: python包是从C++包中编译出来的,所以需要将源码下载进行编译安装 1.GDAL中的矢量数据处理OGR依赖于Geos,在安装GDAL之前要安装Geos Geos的下载地址:http:/ ...

  5. 独立版的 Asio安装与使用

    Asio分为独立版和Boost版.两者使用方法基本一致,只是头文件不同.Boost版是作为Boost的子库提供的. 因为Asio的组织形式为hpp文件(不同一般的C++项目区分头文件.h和源文件.cp ...

  6. fidder抓包使用(一)

    fidder是会占用 jupyter 端口的,在fidder里边最上边找到tools--->options-->connections里边的8888改成别的重启jupyter就好了

  7. 常见任务&基本工具 1 软件包管理

    打包系统主要有两个阵营 包文件的简介 Package files are created by a person known as a package maintainer, often (but n ...

  8. day18 14.连接池介绍

    数据源在软件编程行业有两种概念:一种数据源指的是存储数据的源头(数据库啊文件啊叫数据源),一种指的是连接池(连接池的英文单词叫做DataSource,直译就是数据源).数据源可以指数据库,也可以指连接 ...

  9. leetcode 21-30 easy

    21. Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new list s ...

  10. Lavavel 程序报错 MassAssignmentException in Model.php line 452: _token

    Lavarel 用类似于下面命令插入数据时候出错 Comment::create($request->all()) 错误页面截图如下: 错误原因:下面这行代码应该写到对应的Model里面,而不是 ...