题目:

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. 使用Python的requests库作接口测试——请求对象与响应对象

    任何时候调用requests.*()操作接口时,我们都在做两件事情: 1.构建一个Request对象,该对象被发送到服务器去请求或查询一些资源: 2.一旦requests得到一个从服务器返回的响应,就 ...

  2. 好用的Win10快捷键

    好用的Win10快捷键 Top 01 基础按键 Win+E: 打开"资源管理器". Win+R: 打开"运行"对话框. Win+L: 锁定当前用户. Win+D ...

  3. Django项目:CRM(客户关系管理系统)--40--32PerfectCRM实现King_admin添加不进行限制

    # forms.py # ————————19PerfectCRM实现King_admin数据修改———————— from django import forms from crm import m ...

  4. laravel--设置不需要csrfToken校验的接口

    一般是前后端分离或者支付宝响应的时候需要不设置csrfToken校验的接口,laravel推荐使用passport安全校验设计接口

  5. androidstudio实现增量更新步骤

    本文demo和参考例子参考-传送  门:http://blog.csdn.net/duguang77/article/details/17676797 一.增量更新优点:节省客户端和服务器端流量 增量 ...

  6. LintCode_453 将二叉树拆成链表

    题目 将一棵二叉树按照前序遍历拆解成为一个假链表.所谓的假链表是说,用二叉树的 right 指针,来表示链表中的 next 指针. 样例 1 \ 1 2 / \ \ 2 5 => 3 / \ \ ...

  7. Python操作数据库遇到的问题

    网上教程很多,不多赘述,记录一下遇到的问题. 开始安装的是Python3.x最新版本,用的是pycharm,教程参考有 https://www.cnblogs.com/yufeihlf/p/60041 ...

  8. 洛谷P2835 刻录光盘 [2017年6月计划 强连通分量02]

    P2835 刻录光盘 题目描述 在JSOI2005夏令营快要结束的时候,很多营员提出来要把整个夏令营期间的资料刻录成一张光盘给大家,以便大家回去后继续学习.组委会觉得这个主意不错!可是组委会一时没有足 ...

  9. bzoj4974: [Lydsy八月月赛]字符串大师

    传送门 题目可转换为已知一个串kmp之后的nxt数组,求字典序最小的原串. 已知第i位结尾的串循环节长度位x,那么nxt[i]=i-x; 当nxt不为0时,s[i]=s[nxt[i]]; nxt为0时 ...

  10. Ajax--解析JSON数据与解析XML数据

    一.Ajax解析JSON数据 nav.json(json数据) [ { "link":"http://www.jd.com", "src": ...