思科校招算法-验证IP地址-Leetcode.468
Quest
Given a string queryIP
, return "IPv4"
if IP is a valid IPv4 address, "IPv6"
if IP is a valid IPv6 address or "Neither"
if IP is not a correct IP of any type.
A valid IPv4 address is an IP in the form "x1.x2.x3.x4"
where 0 <= xi <= 255
and x_i
cannot contain leading zeros. For example, "192.168.1.1"
and "192.168.1.0"
are valid IPv4 addresses while "192.168.01.1", "192.168.1.00", and "[email protected]"
are invalid IPv4 addresses.
A valid IPv6 address is an IP in the form "x1:x2:x3:x4:x5:x6:x7:x8"
where:
1 <= x_i.length <= 4
x_i
is a hexadecimal string which may contain digits, lowercase English letter ('a'
to'f'
) and upper-case English letters ('A'
to'F'
).- Leading zeros are allowed in
x_i
.
For example, "2001:0db8:85a3:0000:0000:8a2e:0370:7334"
and "2001:db8:85a3:0:0:8A2E:0370:7334"
are valid IPv6 addresses, while "2001:0db8:85a3::8A2E:037j:7334"
and "02001:0db8:85a3:0000:0000:8a2e:0370:7334"
are invalid IPv6 addresses.
#include <algorithm>
#include <array>
#include <cctype>
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Solution {
private:
static constexpr size_t MAX_LENGTH = 39;
string strFail = "Neither";
string strIPv4 = "IPv4";
string strIPv6 = "IPv6";
bool checkLength(const string &queryIP) {
if (queryIP.length() > MAX_LENGTH) {
return false;
}
return true;
}
bool checkIPv4Digits(const string &digits) {
// length check
if (digits.empty() || digits.length() > 3) {
return false;
}
// leading zero
if (digits.length() != 1 && digits.at(0) == '0') { // digits.starts_with('0')
return false;
}
// non digit characters
for (auto &c: digits) {
if (isdigit(c) == false) {
return false;
}
}
// value > 255
if (stoi(digits, nullptr, 10) > 255) {
return false;
}
return true;
}
bool checkIPv6Digits(const string &digits) {
// length check
if (digits.empty() || digits.length() > 4) {
return false;
}
// non digit characters
if (digits.find_first_not_of("0123456789abcdefABCDEF", 0) != string::npos) {
return false;
}
return true;
}
bool checkIPv4(string queryIP) {
string delim = ".";
size_t pos = 0;
string token;
vector<string> digits;
while ((pos = queryIP.find(delim)) != string::npos) {
token = queryIP.substr(0, pos);
digits.push_back(token);
queryIP.erase(0, pos + delim.length());
}
token = queryIP;
digits.push_back(token);
for (auto &i: digits) {
if (checkIPv4Digits(i) == false) {
return false;
}
}
return true;
}
bool checkIPv6(string queryIP) {
string delim = ":";
size_t pos = 0;
string token;
vector<string> digits;
while ((pos = queryIP.find(delim)) != string::npos) {
token = queryIP.substr(0, pos);
digits.push_back(token);
queryIP.erase(0, pos + delim.length());
}
token = queryIP;
digits.push_back(token);
for (auto &i: digits) {
if (checkIPv6Digits(i) == false) {
return false;
}
}
return true;
}
public:
string validIPAddress(string queryIP) {
if (checkLength(queryIP) == false) {
return strFail;
}
if (count(queryIP.begin(), queryIP.end(), '.') == 3) {
if (checkIPv4(queryIP) == true) {
return strIPv4;
}
}
else if (count(queryIP.begin(), queryIP.end(), ':') == 7) {
if (checkIPv6(queryIP) == true) {
return strIPv6;
}
}
return strFail;
}
};
template <typename T, typename... N>
auto make_array(N &&...args) -> std::array<T, sizeof...(args)> {
return {std::forward<N>(args)...};
}
int main() {
Solution s;
auto inputs = make_array<string>("172.16.254.1", "2001:0db8:85a3:0:0:8A2E:0370:7334", "256.256.256.256");
for (auto &testCase: inputs) {
cout << s.validIPAddress(testCase) << endl;
}
return EXIT_SUCCESS;
}
Refers
- https://stackoverflow.com/questions/14265581/parse-split-a-string-in-c-using-string-delimiter-standard-c
- https://stackoverflow.com/questions/8899069/how-to-find-if-a-given-string-conforms-to-hex-notation-eg-0x34ff-without-regex
- https://stackoverflow.com/questions/791982/determine-if-a-string-is-a-valid-ipv4-address-in-c