Write a function to check whether an 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, leading zeros in the IPv4 is invalid. For example, the address 172.16.254.01
is invalid.
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 valid 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, extra leading zeros in the IPv6 is also invalid. For example, the address 02001:0db8:85a3:0000:0000:8a2e:0370:7334
is invalid.
Note: You may assume there is no extra space or 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.
思路:其实这种题的思路就是考虑并排除异常情况,这里贴出两种方法,第一种是利用正则表达式判断输入的合法性,第二种是利用函数find_first_not_of()判断输入的合法性。
正则表达式的方法:
class Solution {
private:
vector<string> split(string s, char split_c) {
vector<string> ans;
string tmp;
for (char c : s) {
if (c == split_c) {
ans.push_back(tmp);
tmp.clear();
}
else tmp += c;
}
ans.push_back(tmp);
return ans;
}
bool is_IPv4(string IP) {
vector<string> numbers;
numbers = split(IP, '.');
if (numbers.size() != 4) return false;
regex re("^[0-9]+$");
for (string s : numbers) {
if (!s.size() || s.size()>3) return false;
if(!regex_match(s, re)) return false;
int n = stoi(s);
if (n < 10 && s.size()>1) return false;
else if (n < 100 && s.size()>2) return false;
else if (n > 255) return false;
}
return true;
}
bool is_IPv6(string IP) {
vector<string> numbers;
numbers = split(IP, ':');
if (numbers.size() != 8) return false;
regex re("^[a-fA-F0-9]+$");
for (int i = 0; i < numbers.size(); i++) {
string s = numbers[i];
if (s.size() > 4 || (s.size() == 0)) return false;
if (!regex_match(s, re)) return false;
}
return true;
}
public:
string validIPAddress(string IP) {
if (is_IPv4(IP)) return "IPv4";
else if (is_IPv6(IP)) return "IPv6";
else return "Neither";
}
};
函数的方法:
class Solution {
private:
vector<string> split(string& s, char ch) {
stringstream ss(s);
vector<string> result;
string temp;
//".11.1."这种例子最后会存成["", "11", "1"]
//最后一个delimiter后面如果是空字符串的话不会被读进来
while (getline(ss, temp, ch)) {
result.push_back(temp);
}
return result;
}
bool validIPv4(string& s) {
if (s.empty() || s.back() == '.') return false;
vector<string> tokens = split(s, '.');
if (tokens.size() != 4) return false;
for (string& token : tokens) {
if (token.size() > 3) return false;
if (token.find_first_not_of("0123456789") != string::npos) return false;
if (token.size() > 1 && token[0] == '0') return false;
if (token.empty()) return false;
if (stoi(token) < 0 || stoi(token) > 255) return false;
}
return true;
}
bool validIPv6(string& s) {
if (s.empty() || s.back() == ':') return false;
vector<string> tokens = split(s, ':');
if (tokens.size() != 8) return false;
for (string& token : tokens) {
if (token.find_first_not_of("0123456789abcdefABCDEF") != string::npos) return false;
if (token.size() > 4) return false;
if (token.empty()) return false;
}
return true;
}
public:
string validIPAddress(string IP) {
if (validIPv6(IP)) {
return "IPv6";
} else if (validIPv4(IP)) {
return "IPv4";
} else {
return "Neither";
}
}
};