{"id":1907,"date":"2018-03-01T03:42:20","date_gmt":"2018-03-01T11:42:20","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=1907"},"modified":"2018-03-07T23:50:16","modified_gmt":"2018-03-08T07:50:16","slug":"leetcode-468-validate-ip-address","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-468-validate-ip-address\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 468. Validate IP Address"},"content":{"rendered":"<p>\u9898\u76ee\u5927\u610f\uff1a\u7ed9\u4f60\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u8ba9\u4f60\u5224\u65ad\u662f\u5426\u662f\u5408\u6cd5\u7684ipv4\u6216\u8005ipv6\u5730\u5740\uff0c\u90fd\u4e0d\u5408\u6cd5\u8fd4\u56deNeighter.<\/p>\n<p>Write a function to check whether an input string is a valid IPv4 address or IPv6 address or neither.<\/p>\n<p><b>IPv4<\/b>\u00a0addresses are canonically represented in dot-decimal notation, which consists of four decimal numbers, each ranging from 0 to 255, separated by dots (&#8220;.&#8221;), e.g.,<code>172.16.254.1<\/code>;<\/p>\n<p>Besides, leading zeros in the IPv4 is invalid. For example, the address\u00a0<code>172.16.254.01<\/code>\u00a0is invalid.<\/p>\n<p><b>IPv6<\/b>\u00a0addresses are represented as eight groups of four hexadecimal digits, each group representing 16 bits. The groups are separated by colons (&#8220;:&#8221;). For example, the address\u00a0<code>2001:0db8:85a3:0000:0000:8a2e:0370:7334<\/code>\u00a0is a valid one. Also, we could omit some leading zeros among four hexadecimal digits and some low-case characters in the address to upper-case ones, so\u00a0<code>2001:db8:85a3:0:0:8A2E:0370:7334<\/code>\u00a0is also a valid IPv6 address(Omit leading zeros and using upper cases).<\/p>\n<p>However, we don&#8217;t replace a consecutive group of zero value with a single empty group using two consecutive colons (::) to pursue simplicity. For example,\u00a0<code>2001:0db8:85a3::8A2E:0370:7334<\/code>\u00a0is an invalid IPv6 address.<\/p>\n<p>Besides, extra leading zeros in the IPv6 is also invalid. For example, the address\u00a0<code>02001:0db8:85a3:0000:0000:8a2e:0370:7334<\/code>\u00a0is invalid.<\/p>\n<p><b>Note:<\/b>\u00a0You may assume there is no extra space or special characters in the input string.<\/p>\n<p><b>Example 1:<\/b><\/p>\n<pre><b>Input:<\/b> \"172.16.254.1\"\r\n\r\n<b>Output:<\/b> \"IPv4\"\r\n\r\n<b>Explanation:<\/b> This is a valid IPv4 address, return \"IPv4\".\r\n<\/pre>\n<p><b>Example 2:<\/b><\/p>\n<pre><b>Input:<\/b> \"2001:0db8:85a3:0:0:8A2E:0370:7334\"\r\n\r\n<b>Output:<\/b> \"IPv6\"\r\n\r\n<b>Explanation:<\/b> This is a valid IPv6 address, return \"IPv6\".\r\n<\/pre>\n<p><b>Example 3:<\/b><\/p>\n<pre class=\"\"><b>Input:<\/b> \"256.256.256.256\"\r\n\r\n<b>Output:<\/b> \"Neither\"\r\n\r\n<b>Explanation:<\/b> This is neither a IPv4 address nor a IPv6 address.<\/pre>\n<p><strong>Solution 1: String \/ Brute force\u00a0<\/strong><\/p>\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 4 ms\r\nclass Solution {\r\npublic:\r\n  bool isV4Num(string n) {\r\n    if (n.length() &gt; 3 || n.empty()) return false;\r\n    std::reverse(n.begin(), n.end());\r\n    int a = 0;    \r\n    int f = 1;\r\n    for (char c : n) {\r\n      if (!isdigit(c)) return false;\r\n      if (c == '0' &amp;&amp; f &gt; 1) return false;\r\n      a += (c - '0') * f;\r\n      f *= 10;\r\n    }\r\n    return a &gt;= 0 &amp;&amp; a &lt;= 255;\r\n  }\r\n  \r\n  bool isV6Num(string n) {\r\n    if (n.length() &gt; 4 || n.empty()) return false;\r\n    std::reverse(n.begin(), n.end());\r\n    \r\n    int a = 0;\r\n    int f = 1;    \r\n    for (char c : n) {      \r\n      if (isdigit(c)) \r\n        a += (c - '0') * f;\r\n      else if (c &gt;= 'a' &amp;&amp; c &lt;= 'f' || c &gt;= 'A' &amp;&amp; c &lt;= 'Z')\r\n        a += (tolower(c) - 'a' + 10) * f;\r\n      else\r\n        return false;\r\n      f *= 16;\r\n    }\r\n    \r\n    return a &gt;= 0 &amp;&amp; a &lt; (1 &lt;&lt; 16);\r\n  }\r\n  \r\n  string validIPAddress(string IP) {\r\n    bool isIPv4 = true;\r\n    bool isIPv6 = true;\r\n    string cur;\r\n    int seg = 0;\r\n    for (char c: IP) {\r\n      if (c == '.') { ++seg; isIPv6 = false; isIPv4 &amp;= isV4Num(cur); cur.clear(); }\r\n      else if (c == ':') { ++seg; isIPv4 = false; isIPv6 &amp;= isV6Num(cur); cur.clear(); }\r\n      else cur += c;\r\n    }\r\n    isIPv4 &amp;= isV4Num(cur) &amp;&amp; seg == 3;\r\n    isIPv6 &amp;= isV6Num(cur) &amp;&amp; seg == 7;\r\n    \r\n    return isIPv4 ? \"IPv4\" : (isIPv6 ? \"IPv6\" : \"Neither\");\r\n  }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee\u5927\u610f\uff1a\u7ed9\u4f60\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u8ba9\u4f60\u5224\u65ad\u662f\u5426\u662f\u5408\u6cd5\u7684ipv4\u6216\u8005ipv6\u5730\u5740\uff0c\u90fd\u4e0d\u5408\u6cd5\u8fd4\u56deNeighter. Write a function to check whether an input string is a valid IPv4 address or IPv6 address or neither. IPv4\u00a0addresses are canonically represented in&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[47],"tags":[177,224],"class_list":["post-1907","post","type-post","status-publish","format-standard","hentry","category-string","tag-medium","tag-regex","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1907","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=1907"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1907\/revisions"}],"predecessor-version":[{"id":1909,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1907\/revisions\/1909"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=1907"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=1907"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=1907"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}