[LeetCode] Validate IP Address 验证IP地址
In this problem, your job to write a function to check whether a input string is a valid IPv4 address or IPv6 address or neither.
IPv4 addresses are canonically represented in dot-decimal notation, which consists of four decimal numbers, each ranging from 0 to 255, separated by dots ("."), e.g.,172.16.254.1
;
Besides, you need to keep in mind that leading zeros in the IPv4 is illegal. For example, the address 172.16.254.01
is illegal.
IPv6 addresses are represented as eight groups of four hexadecimal digits, each group representing 16 bits. The groups are separated by colons (":"). For example, the address 2001:0db8:85a3:0000:0000:8a2e:0370:7334
is a legal one. Also, we could omit some leading zeros among four hexadecimal digits and some low-case characters in the address to upper-case ones, so 2001:db8:85a3:0:0:8A2E:0370:7334
is also a valid IPv6 address(Omit leading zeros and using upper cases).
However, we don't replace a consecutive group of zero value with a single empty group using two consecutive colons (::) to pursue simplicity. For example, 2001:0db8:85a3::8A2E:0370:7334
is an invalid IPv6 address.
Besides, you need to keep in mind that extra leading zeros in the IPv6 is also illegal. For example, the address 02001:0db8:85a3:0000:0000:8a2e:0370:7334
is also illegal.
Note: You could assume there is no extra space in the test cases and there may some special characters in the input string.
Example 1:
Input: "172.16.254.1" Output: "IPv4" Explanation: This is a valid IPv4 address, return "IPv4".
Example 2:
Input: "2001:0db8:85a3:0:0:8A2E:0370:7334" Output: "IPv6" Explanation: This is a valid IPv6 address, return "IPv6".
Example 3:
Input: "256.256.256.256" Output: "Neither" Explanation: This is neither a IPv4 address nor a IPv6 address.
这道题让我们验证两种IP地址,LeetCode之前有一道关于IPv4的题Restore IP Addresses,给我们了一个字符串,让我们通过在中间加点来找出所有正确的IP地址,这道题给了我们中间加点或者冒号的字符串,让我们验证其是否是正确的IPv4或者IPv6,感觉要稍稍复杂一些。那么我们只有分别来验证了,那么我们怎么样能快速的区别是IPv4或者IPv6呢,当然是通过中间的点或者冒号啦,所以我们首先在字符串中找冒号(当然你想找点也可以),如果字符串中没有冒号,那么我们来验证其是否是IPv4,如果有冒号,我们就来验证其是否是IPv6.
首先对于IPv4,我们使用getline函数来截取两个点之间的字符串,我们还需要一个计数器cnt来记录我们已经截取了多少段,如果cnt大于4了,说明超过了4段,说明是不是正确的地址。如果取出的字符串为空,说明两个点连在一起了,也不对。再有就是如果字符串长度大于1,且第一个字符是0,也不对。由于IPv4的地址在0到255之间,所以如果字符串长度大于3,也不正确。下面我们检查每一个字符,如果有不是数字的字符,返回Neither。最后我们再把字符串转为数字,如果不在0到255之间就是非法的。最后的最后,我们要保证cnt正好为4,而且最后一个字符不能是点,统统满足以上条件才是正确的IPv4地址。
然后对于IPv6,我们也使用getline函数来截取两个冒号之间的字符串,我们同样需要计数器cnt来记录我们已经截取了多少段,如果cnt大于8了,说明超过了8段,说明是不是正确的地址。如果取出的字符串为空,说明两个冒号连在一起了,也不对。面我们检查每一个字符,正确的字符应该是0到9之间的数字,或者a到f,或A到F之间的字符,如果出现了其他字符,返回Neither。最后的最后,我们要保证cnt正好为8,而且最后一个字符不能是冒号,统统满足以上条件才是正确的IPv6地址。
class Solution {
public:
string validIPAddress(string IP) {
istringstream is(IP);
string t = "";
int cnt = ;
if (IP.find(':') == string::npos) { // Check IPv4
while (getline(is, t, '.')) {
++cnt;
if (cnt > || t.empty() || (t.size() > && t[] == '') || t.size() > ) return "Neither";
for (char c : t) {
if (c < '' || c > '') return "Neither";
}
int val = stoi(t);
if (val < || val > ) return "Neither";
}
return (cnt == && IP.back() != '.') ? "IPv4" : "Neither";
} else { // Check IPv6
while (getline(is, t, ':')) {
++cnt;
if (cnt > || t.empty() || t.size() > ) return "Neither";
for (char c : t) {
if (!(c >= '' && c <= '') && !(c >= 'a' && c <= 'f') && !(c >= 'A' && c <= 'F')) return "Neither";
}
}
return (cnt == && IP.back() != ':') ? "IPv6" : "Neither";
}
}
};
类似题目:
参考资料:
https://discuss.leetcode.com/topic/71572/java-solution
https://discuss.leetcode.com/topic/71418/short-regexp-solution/5
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] Validate IP Address 验证IP地址的更多相关文章
- 468 Validate IP Address 验证IP地址
详见:https://leetcode.com/problems/validate-ip-address/description/ Java实现: class Solution { public St ...
- [Leetcode] restore ip address 存储IP地址
Given a string containing only digits, restore it by returning all possible valid IP address combina ...
- [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 ...
- IP池验证IP是否可用 及scrapy使用 ip池
简单验证 import requests url = "http://www.baidu.com/"proxies = {"http": "http: ...
- 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】468. Validate IP Address 解题报告(Python)
[LeetCode]468. Validate IP Address 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: h ...
- Java实现 LeetCode 468 验证IP地址
468. 验证IP地址 编写一个函数来验证输入的字符串是否是有效的 IPv4 或 IPv6 地址. IPv4 地址由十进制数和点来表示,每个地址包含4个十进制数,其范围为 0 - 255, 用(&qu ...
随机推荐
- [Algorithm] 使用SimHash进行海量文本去重
在之前的两篇博文分别介绍了常用的hash方法([Data Structure & Algorithm] Hash那点事儿)以及局部敏感hash算法([Algorithm] 局部敏感哈希算法(L ...
- Hadoop入门学习笔记---part4
紧接着<Hadoop入门学习笔记---part3>中的继续了解如何用java在程序中操作HDFS. 众所周知,对文件的操作无非是创建,查看,下载,删除.下面我们就开始应用java程序进行操 ...
- docker进入后台运行的容器
转载请注明出处 我们运行docker容器的时候,使用了-d参数,把容器在后台运行后. 这个时候,我们使用docker ps命令,我们就可以知道哪些程序在后台运行. 我们要怎么进入到docker ...
- Windows下Memcached安装与配置实例
环境声明: 服务器: Windows Server 2008r2: Memcached: Memcached 64-bit for Windows(64位) From: http://www.urie ...
- Windows 10 安装 Sql Server 2014 反复提示需要安装 .NET Framework 3.5 SP1 的解决方案
一.首先安装.NET Framework 3.5: 离线安装方式: 1.装载相对应的系统安装盘,我是Windows 10 x64 企业版,所以装载Windows 10 x64 企业版安装镜像ISO,盘 ...
- Servlet 服务器性能提高--->数据库请求频率控制(原创)
首先我要说下我实现这个功能接口涉及到的业务和实现的详细流程,然后会说此接口涉及到的相关技术,最后会贴出注释后的详细代码, 这个接口涉及到的是 app上咻一咻功能,咻一咻中奖的奖品一共有七类,其中四类是 ...
- HTML5 数据集属性dataset
有时候在HTML元素上绑定一些额外信息,特别是JS选取操作这些元素时特别有帮助.通常我们会使用getAttribute()和setAttribute()来读和写非标题属性的值.但为此付出的代价是文档将 ...
- 使用WebRTC搭建前端视频聊天室——信令篇
博客原文地址 建议看这篇之前先看一下使用WebRTC搭建前端视频聊天室——入门篇 如果需要搭建实例的话可以参照SkyRTC-demo:github地址 其中使用了两个库:SkyRTC(github地址 ...
- Android 5.X新特性之为RecyclerView添加下拉刷新和上拉加载及SwipeRefreshLayout实现原理
RecyclerView已经写过两篇文章了,分别是Android 5.X新特性之RecyclerView基本解析及无限复用 和 Android 5.X新特性之为RecyclerView添加Header ...
- ASP.NET MVC 让@Html.DropDownList显示默认值
在使用@Html.DropDownList的过程中,发现它的用法很局限,比如在加载的时候显示设定的默认项或者调整它的显示样式,在网上查了一些资料,终于把这个问题解决了. 一.View代码 @using ...