标签:识别 git http height ring int 技术 com 误判
题目:
解答:
1 class Solution { 2 public: 3 string validIPAddress(string IP) 4 { 5 if (isValidIPv4(IP)) 6 { 7 return "IPv4"; 8 } 9 if (isValidIPv6(IP)) 10 { 11 return "IPv6"; 12 } 13 return "Neither"; 14 } 15 16 // 优雅的split 17 void split(const string s, vector<string>& vs, const char delim= ‘ ‘) 18 { 19 istringstream iss(s); 20 string temp; 21 while (getline(iss,temp,delim)) 22 { 23 vs.emplace_back(move(temp)); 24 } 25 if (!s.empty() && s.back() == delim) 26 { 27 vs.push_back({}); 28 //加这句的原因是getline不会识别最后一个delim,避免误判"172.16.254.1.","2001:0db8:85a3:0:0:8A2E:0370:7334:"之类的情况 29 } 30 } 31 32 // 判定是否IPv4 33 bool isValidIPv4(string IP) 34 { 35 vector<string> vs; 36 split(IP,vs,‘.‘); 37 if (vs.size()!=4) 38 { 39 return false; 40 } 41 42 for (auto &v:vs) 43 { 44 if (v.empty() || (v.size()>1 && v[0] == ‘0‘) || v.size()>3) 45 { 46 return false; 47 } 48 for (auto c:v) 49 { 50 if (!isdigit(c)) 51 { 52 return false; 53 } 54 } 55 int n = stoi(v); 56 if (n<0||n>255) 57 { 58 return false; 59 } 60 } 61 62 return true; 63 } 64 65 // 判定是否IPv6 66 bool isValidIPv6(string IP) 67 { 68 vector<string> vs; 69 split(IP,vs,‘:‘); 70 if (vs.size()!=8) 71 { 72 return false; 73 } 74 for (auto &v:vs) 75 { 76 if (v.empty() || v.size()>4 ) 77 { 78 return false; 79 } 80 for (auto c:v) 81 { 82 if (!(isdigit(c) || (c>=‘a‘&&c<=‘f‘) || (c>=‘A‘&&c<=‘F‘))) 83 { 84 return false; 85 } 86 } 87 } 88 89 return true; 90 91 } 92 };
标签:识别 git http height ring int 技术 com 误判
原文地址:https://www.cnblogs.com/ocpc/p/12826037.html