[Leetcode] restore ip address 存储IP地址
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)
题意:给定一由纯数字组成的字符串,以IP地址的形式存储。
思路:首先要明白IP地址的书写形式。IP地址由32位二进制数组成,常以XXX.XXX.XXX.XXX形式表现,每组XXX代表小于或等于255的10进制数,故IP地址总共有四段,每一段可能有一到三位,范围是[0, 255],题目明确指出输入字符串只含有数字。
两点要注意:一、当某段是三位时,我们要判断其是否越界(>255);二、当只有一位时,0可以成某一段,如果有两位或三位时,像 00, 01, 001, 011, 000等都是不合法的。
可以看做是字符串的分段问题,在输入字符串中加入三个点,将字符串分为四段,每一段都要是合法的(条件见上面两点)。使用递归法,我们用k来表示当前还需要分的段数,终止条件是当如果k = 0(则表示三个点已经加入完成,四段已经形成),若这时字符串刚好为空即s.empty(),这种个字符串是合法的。若k != 0, 则对于每一段,我们分别用一位,两位,三位来尝试,分别判断其合不合法,如果合法,则调用递归继续分剩下的字符串,最终和求出所有合法组合。
参考了Grandyang的博客。代码如下:
class Solution {
public:
vector<string> restoreIpAddresses(string s)
{
vector<string> res;
restore(s,res,,"");
return res;
}
void restore(string s,vector<string> &res,int k,string str)
{
if(k==&&s.empty())
res.push_back(str);
else
{
for(int i=;i<;++i)
{
if(s.size()>=i&&isValid(s.substr(,i)))
{
if(k==)
restore(s.substr(i),res,k-,str+s.substr(,i));
else
restore(s.substr(i),res,k-,str+s.substr(,i)+".");
}
}
}
}
bool isValid(string s)
{
if(s.size()>||s.empty()||(s.size()>&&s[]==''))
return false;
int val=atoi(s.c_str());
return val>=&&val<=;
}
};
可以将第20~23行简写为如下:
restore(s.substr(i),k-,str+s.substr(,i)+(k==?"":"."),res);
[Leetcode] restore ip address 存储IP地址的更多相关文章
- [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 ...
- [LintCode] Restore IP Address 复原IP地址
Given a string containing only digits, restore it by returning all possible valid IP address combina ...
- 华东师大OJ:IP Address【IP地址转换】
/*===================================== IP Address Time Limit:1000MS Memory Limit:30000KB Total Subm ...
- 468 Validate IP Address 验证IP地址
详见:https://leetcode.com/problems/validate-ip-address/description/ Java实现: class Solution { public St ...
- Windows Azure Cloud Service (44) 将Cloud Service加入Virtual Network Subnet,并固定Virtual IP Address(VIP)
<Windows Azure Platform 系列文章目录> 在之前的文章中,笔者已经详细介绍了如何将Virtual Machine加入Virtual Network,并且绑定固定的Pr ...
- VIP - virtual IP address
virtual IP address (虚拟 IP 地址)1.是集群的ip地址,一个vip对应多个机器2.与群集关联的唯一 IP 地址 see wiki: A virtual IP address ( ...
- LeetCode OJ:Restore IP Addresses(存储IP地址)
Given a string containing only digits, restore it by returning all possible valid IP address combina ...
- [LeetCode] Restore IP Addresses 复原IP地址
Given a string containing only digits, restore it by returning all possible valid IP address combina ...
- LeetCode:Restore IP Address
93. Restore IP Addresses Given a string containing only digits, restore it by returning all possible ...
随机推荐
- linux 安装 node.js
wget http://nodejs.org/dist/v0.10.26/node-v0.10.26.tar.gztar zxvf node-v0.10.26.tar.gzcd node-v0.10. ...
- OSG-漫游
本文转至http://www.cnblogs.com/shapherd/archive/2010/08/10/osg.html 作者写的比较好,再次收藏,希望更多的人可以看到这个文章 互联网是是一个相 ...
- JavaScript实现无刷新评论及在IE下的剪切板访问(学习)
1.无刷新评论 tips: appendChild:将新元素作为父元素的最后一个子元素进行添加. insertBefore:在一个指定的子节点之前插入一个节点 实现: <!DOCTYPE htm ...
- 复合词 (Compund Word,UVa 10391)
题目描述: 题目思路: 用map保存所有单词赋键值1,拆分单词,用map检查是否都为1,即为复合词 #include <iostream> #include <string> ...
- Windows10安装GPU版本的Tensorflow
本人电脑配置(公司的)gtx1080ti,下载的的cuda8.0,cudnn6.0,python3.5.3安装完成后,安装tensorflow 1.pip install tensorflow-gpu ...
- yun rpm
RPM:RedHat Package Manager的简称,是一种数据库记录的方式的管理机制.当需要安装的软件的依赖软件都已经安装,则继续安装,否则不予安装. 特点:1.已经编译并打包完成2.软件的信 ...
- solidity 智能合约操作
合约编译 #!/usr/bin/env python # coding: utf8 import json import os # Solc Compiler from functools impor ...
- 基于Hadoop2.5.0的集群搭建
http://download.csdn.net/download/yameing/8011891 一. 规划 1. 准备安装包 JDK:http://download.oracle.com/otn ...
- 有道云笔记Markdown使用
目录 使用规则 代码高亮 制作待办事项 高效绘图 基本规则 使用规则 代码高亮 #include <iostream> #include <string> using name ...
- Fafa and the Gates(模拟)
Two neighboring kingdoms decided to build a wall between them with some gates to enable the citizens ...