思科校招算法-验证IP地址-Leetcode.468


思科校招算法-验证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.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#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

文章作者: sfc9982
版权声明: 本博客所有文章除特別声明外,均采用 CC BY-NC-ND 4.0 许可协议。转载请注明来源 sfc9982 !
  目录