93. Restore IP Addresses

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

For example:
Given "25525511135",

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

Tags: Backtracking String

输入一个字符串,要求输出该字符串可能拆分成的ip地址形式。

思路:ipv4的地址应该是4段,每段0-255中的一个数。所以考虑4层DFS加一些剪枝条件即可。剪枝条件有:1、取3个字符串stoi作为地址时大于255;2、2位数或者3位数地址段以0开头

另外要检查第四段ip地址为0的情况,防止类似于"0000000"atoi之后也算作0的情况。

 class Solution {
public: bool dfs(string input, int number, string ipAddress, vector<string> &result){
if(input.length() == ){
return false;
}
if(number == ){
int addressNumber = stoi(input);
if(input[] == ''){
if(!(input.length() == && addressNumber == ))
return false;
}
if(addressNumber <= ){
ipAddress = ipAddress + input;
result.push_back(ipAddress);
return true;
}else{
return false;
}
}else{
if(input.length() >= ){
dfs(input.substr(), number + , ipAddress + input.substr(, ) + ".", result);
}
if(input.length() >= && input[] != ''){
dfs(input.substr(), number + , ipAddress + input.substr(, ) + ".", result);
}
if(input.length() >= && input[] != ''){
int addressNumber = stoi(input.substr(, ));
if(addressNumber <= ){
dfs(input.substr(), number + , ipAddress + input.substr(, ) + ".", result);
}
}
}
return true;
} vector<string> restoreIpAddresses(string s) {
vector<string> result;
if(s.length() == || s.length() > ){
return result;
}
string temp = "";
dfs(s, , temp, result);
return result;
}
};

LeetCode:Restore IP Address的更多相关文章

  1. [Leetcode] restore ip address 存储IP地址

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

  2. [LeetCode] Restore IP Address [28]

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

  3. [LeetCode] Validate IP Address 验证IP地址

    In this problem, your job to write a function to check whether a input string is a valid IPv4 addres ...

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

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

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

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

  6. [leetcode]Restore IP Addresses @ Python

    原题地址:https://oj.leetcode.com/problems/restore-ip-addresses/ 题意: Given a string containing only digit ...

  7. LeetCode——Restore IP Addresses

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

  8. LeetCode: Restore IP Addresses 解题报告

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

  9. [LeetCode] Restore IP Addresses 回溯

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

随机推荐

  1. git fork同步原作者

    从github上获取源代码,一种是直接下载,但是无法改动后提交. 一种是fork一下,但是和原作者同步麻烦. 所以我找到了四个命令,解决同步问题. 以后建议大家fork一下,主要是哪天对源码熟悉了,想 ...

  2. linux内核驱动模型

    linux内核驱动模型,以2.6.32内核为例.(一边写一边看的,有点乱.) 1.以内核对象为基础.用kobject表示,相当于其它对象的基类,是构建linux驱动模型的关键.具有相同类型的内核对象构 ...

  3. HttpContext

    HttpContext 类:封装有关个别 HTTP 请求的所有 HTTP 特定的信息.也有人叫上下文信息. 1.生存周期:从客户端用户点击并产生了一个向服务器发送请求开始---服务器处理完请求并生成返 ...

  4. asp.net mvc 事务处理:Transactions

    1.在控制器里引用using System.Transactions; 2.在你需要事务回滚的地方外面套一层using (TransactionScope sc = new TransactionSc ...

  5. add a path cgi-bin to asp.net mvc

    1.简单,但是会丢失请求数据 protected void Application_BeginRequest() { string url = HttpContext.Current.Request. ...

  6. 微信web开发者工具调试

    微信web开发者工具调试 前几天写了一篇使用fiddler调试微信端页面的,然后博友评论说使用fiddler太麻烦了,推荐使用微信web开发者工具调试微信页面,这两天弄着玩了一下,很强大.这篇文章只是 ...

  7. Memcached(四)Memcached的CAS协议

    1. 什么是CAS协议很多中文的资料都不会告诉大家CAS的全称是什么,不过一定不要把CAS当作中国科学院(China Academy of Sciences)的缩写.Google.com一下,CAS是 ...

  8. Almeza MultiSet Pro(批量安装程序) V8.7.6中文特别版

    Almeza MultiSet Pro(批量安装程序)是一款非常实用的工具.它能够帮你批量地安装常用的软件.这将解决每次重装系统后能够快速方便地重装常用软件.使用这款软件不需要编写程序,还可以在安装过 ...

  9. SQL 中With as 的用法

    转自:http://www.cnblogs.com/superyinhai/archive/2010/04/09/1708643.html 一.WITH AS的含义 WITHAS短语,也叫做子查询部分 ...

  10. Storm技术结合

    http://pan.baidu.com/s/1mhzj5XI?qq-pf-to=pcqq.group#path=%252F