题目:

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. Luogu P4768 [NOI2018]归程(Dijkstra+Kruskal重构树)

    P4768 [NOI2018]归程 题面 题目描述 本题的故事发生在魔力之都,在这里我们将为你介绍一些必要的设定. 魔力之都可以抽象成一个 \(n\) 个节点. \(m\) 条边的无向连通图(节点的编 ...

  2. android 数据绑定(2)绑定表达式

    1.官方文档 https://developer.android.com/topic/libraries/data-binding/expressions.html 2.绑定表达式的约束 2.1 允许 ...

  3. 【python之路36】进程、线程、协程相关

    线程详细用法请参考:http://www.cnblogs.com/sunshuhai/articles/6618894.html 一.初始多线程 通过下面两个例子的运行效率,可以得知多线程的速度比单线 ...

  4. script节点上的onload和onreadystatechange事件

    http://dafeizizhu.github.io/2013/11/25/onload-vs-onreadystatechange/ http://www.cnblogs.com/snandy/a ...

  5. 关闭浏览器或者关闭使用window.open打开的页面时添加监听事件

    最近工作中有个需求:点击按钮时打开一个页面,此处取名为page1,打开页面的前提条件是如果有人已经打开过page1页面并且没有关闭时请求ajax判断session是否为空,如果为空则将用户名和文档id ...

  6. JS random函数深入理解(转载)

    转载自:(本文对读者有帮助的话请移步支持原作者) http://www.cnblogs.com/starof/p/4988516.html 一.预备知识 Math.ceil();  //向上取整. M ...

  7. 20190722-Moni和Boly的故事

    Moni(模拟)可以得到60分 Boly(暴力)可以得到40分 还好说,这次有点可行. 过程是: 先看了T3,可以模拟,然后做T1T2 T1 好说,$exgcd$,不会. 暴力,暴力!骗了40. T2 ...

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

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

  9. webpack学习之—— Manifest

    Runtime runtime,以及伴随的 manifest 数据,主要是指:在浏览器运行时,webpack 用来连接模块化的应用程序的所有代码.runtime 包含:在模块交互时,连接模块所需的加载 ...

  10. sprite学习

    CSS雪碧图,就是把所有的图表,按钮和图形包含在一个图像里面.它要求: 静态图片,不随用户信息变化而变化:小图片,图片容量比较小:加载量比较大. 使用这种技术可以减少Web浏览器发出的服务器请求,显著 ...