forked from AlgoStudyGroup/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathValidate-IP-Address.cpp
73 lines (67 loc) · 2.27 KB
/
Validate-IP-Address.cpp
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
class Solution {
public:
void split(string str, char delim, vector<string>& ans) {
int current, previous = 0;
current = str.find(delim);
ans.resize(0);
while (current != std::string::npos) {
ans.push_back(str.substr(previous, current - previous));
previous = current + 1;
current = str.find(delim, previous);
}
ans.push_back(str.substr(previous, current - previous));
}
bool is_IP4(vector<string>& s){
if (s.size() != 4) return false;
for (int i = 0; i < 4; i++){
if (s[i].size() == 0 or s[i].size() > 3 or (s[i][0] == '0' and s[i].size() > 1)) return false;
for (auto& c: s[i]) if (!isdigit(c)) return false;
int tmp = stoi(s[i]);
if (tmp < 0 or tmp > 255) return false;
}
return true;
}
bool is_IP6(vector<string>& s){
if (s.size() != 8) return false;
for (int i = 0; i < 8; i++){
if (s[i].size() == 0 or s[i].size() > 4) return false;
for (auto& c: s[i]) {
c = toupper(c);
if (isdigit(c)) continue;
if (c >= 'A' and c <= 'F') continue;
return false;
}
}
return true;
}
string validIPAddress(string IP) {
vector<string> s;
split(IP, '.', s);
if (is_IP4(s)) return "IPv4";
split(IP, ':', s);
if (is_IP6(s)) return "IPv6";
return "Neither";
}
};
class Solution2 {
public:
string validIPAddress(string IP) {
if (IP.find('.') != string::npos) {
regex ipv4_regex("((2[0-4]\\d|25[0-5]|0|[1-9]\\d?|1\\d\\d)\\.){3}(2[0-4]\\d|25[0-5]|0|[1-9]\\d?|1\\d\\d)");
// 2[0-4]\\d 200~249
// 25[0-5] 250~255
// 0 0
// [1-9]\\d? 1~99
// 1\\d\\d 100~199
if (regex_match(IP, ipv4_regex))
return "IPv4";
}
else if (IP.find(':') != string::npos) {
regex ipv6_regex("([\\da-fA-F]{1,4}:){7}([\\da-fA-F]{1,4})");
// [\\da-fA-F]{1,4} 0000~FFFF
if (regex_match(IP, ipv6_regex))
return "IPv6";
}
return "Neither";
}
};