原题链接
class Solution {
public:
bool checkv4(string ip){
if(ip.size() > 15) return false;
int num = 0;
for(int i = 0, j = 0; i < ip.size();){
string word = "";
if(j < ip.size() && ip[j] == '.') j ++;
while(j < ip.size() && ip[j] != '.') word += ip[j ++];
if(word.size() == 0 || word.size() > 4 || (word.size() > 1 && word[0] == '0') ) return false;
for(auto c: word){
if(c >= '0' && c <= '9') continue;
else return false;
}
if(stoi(word) >= 256) return false;
i = j;
if(ip[i] == '.') num ++;
}
if(num != 3) return false;
return true;
}
bool checkv6(string ip){
if(ip.size() > 39) return false;
int num = 0;
for(int i = 0, j = 0; i < ip.size();){
string word = "";
if(j < ip.size() && ip[j] == ':') j ++;
while(j < ip.size() && ip[j] != ':') word += ip[j ++];
if(word.size() == 0 || word.size() > 4) return false;
for(auto c: word){
if((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F')) continue;
else return false;
}
i = j;
if(ip[i] == ':') num ++;
}
if(num != 7) return false;
return true;
}
string validIPAddress(string queryIP) {
if(checkv4(queryIP)) return "IPv4";
if(checkv6(queryIP)) return "IPv6";
return "Neither";
}
};



