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.html
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
;java
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.git
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).ide
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.post
Note: You could assume there is no extra space in the test cases and there may some special characters in the input string.this
Example 1:url
Input: "172.16.254.1" Output: "IPv4" Explanation: This is a valid IPv4 address, return "IPv4".
Example 2:spa
Input: "2001:0db8:85a3:0:0:8A2E:0370:7334" Output: "IPv6" Explanation: This is a valid IPv6 address, return "IPv6".
Example 3:code
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 = 0; if (IP.find(':') == string::npos) { // Check IPv4 while (getline(is, t, '.')) { ++cnt; if (cnt > 4 || t.empty() || (t.size() > 1 && t[0] == '0') || t.size() > 3) return "Neither"; for (char c : t) { if (c < '0' || c > '9') return "Neither"; } int val = stoi(t); if (val < 0 || val > 255) return "Neither"; } return (cnt == 4 && IP.back() != '.') ? "IPv4" : "Neither"; } else { // Check IPv6 while (getline(is, t, ':')) { ++cnt; if (cnt > 8 || t.empty() || t.size() > 4) return "Neither"; for (char c : t) { if (!(c >= '0' && c <= '9') && !(c >= 'a' && c <= 'f') && !(c >= 'A' && c <= 'F')) return "Neither"; } } return (cnt == 8 && IP.back() != ':') ? "IPv6" : "Neither"; } } };
相似题目:
参考资料:
https://discuss.leetcode.com/topic/71572/java-solution
https://discuss.leetcode.com/topic/71418/short-regexp-solution/5