[LeetCode] 468. Validate IP Address

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 xi 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 "192.168@1.1" are invalid IPv4 addresses.

A valid IPv6 address is an IP in the form "x1:x2:x3:x4:x5:x6:x7:x8" where:

  • 1 <= xi.length <= 4
  • xi 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 xi.

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.

Example 1:

Input: queryIP = "172.16.254.1"
Output: "IPv4"
Explanation: This is a valid IPv4 address, return "IPv4".

Example 2:

Input: queryIP = "2001:0db8:85a3:0:0:8A2E:0370:7334"
Output: "IPv6"
Explanation: This is a valid IPv6 address, return "IPv6".

Example 3:

Input: queryIP = "256.256.256.256"
Output: "Neither"
Explanation: This is neither a IPv4 address nor a IPv6 address.

Constraints:

  • queryIP consists only of English letters, digits and the characters '.' and ':'.

验证IP地址。

给定一个字符串 queryIP。如果是有效的 IPv4 地址,返回 "IPv4" ;如果是有效的 IPv6 地址,返回 "IPv6" ;如果不是上述类型的 IP 地址,返回 "Neither" 。

有效的IPv4地址 是 “x1.x2.x3.x4” 形式的IP地址。 其中 0 <= xi <= 255 且 xi 不能包含 前导零。例如: “192.168.1.1” 、 “192.168.1.0” 为有效IPv4地址, “192.168.01.1” 为无效IPv4地址; “192.168.1.00” 、 “192.168@1.1” 为无效IPv4地址。

一个有效的IPv6地址 是一个格式为“x1:x2:x3:x4:x5:x6:x7:x8” 的IP地址,其中:

1 <= xi.length <= 4
xi 是一个 十六进制字符串 ,可以包含数字、小写英文字母( 'a' 到 'f' )和大写英文字母( 'A' 到 'F' )。
在 xi 中允许前导零。
例如 "2001:0db8:85a3:0000:0000:8a2e:0370:7334" 和 "2001:db8:85a3:0:0:8A2E:0370:7334" 是有效的 IPv6 地址,而 "2001:0db8:85a3::8A2E:037j:7334" 和 "02001:0db8:85a3:0000:0000:8a2e:0370:7334" 是无效的 IPv6 地址。

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/validate-ip-address
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

题意很简单。这个题需要验证两种IP地址,IPv4和IPv6。两者的区别如下

IPv4

  • 有三个点
  • 三个点把IP地址分成了四段
  • 每一段地址长度不超过4
  • 每一段是一个介于0 - 255之间的纯数字,不包含 leading zero

IPv6

  • 有七个冒号
  • 七个冒号把IP地址分成了八段
  • 每一段地址长度不超过4
  • 每一段可以由数字和字母的组合构成,数字的范围是在0 - 9,字母的范围是在a - f和A - F之间

有了这些规则,代码就会比较容易实现了,需要多练。这道题有一个环节稍微麻烦一点,就是如何判断数字是否有leading zero的情形,注意代码40行,这里Java利用了string和integer的相互转换,这样如果有leading zero的话,integer转string之后就会跟原来的string不一样了。

时间O(n) - input length

空间O(1)

Java实现

 1 class Solution {
 2     public String validIPAddress(String queryIP) {
 3         // corner case
 4         if (queryIP == null || queryIP.length() == 0) {
 5             return "Neither";
 6         }
 7         if (isIPv4(queryIP)) {
 8             return "IPv4";
 9         }
10         if (isIPv6(queryIP)) {
11             return "IPv6";
12         }
13         return "Neither";
14     }
15     
16     private boolean isIPv4(String ip) {
17         int count = 0;
18         for (char c : ip.toCharArray()) {
19             if (c == '.') {
20                 count++;
21             }
22         }
23         if (count != 3) {
24             return false;
25         }
26         String[] fields = ip.split("\\.");
27         if (fields.length != 4) {
28             return false;
29         }
30         for (String field : fields) {
31             if (field.isEmpty() || field.length() > 3) {
32                 return false;
33             }
34             for (int i = 0; i < field.length(); i++) {
35                 if (!Character.isDigit(field.charAt(i))) {
36                     return false;
37                 }
38             }
39             int num = Integer.valueOf(field);
40             if (!String.valueOf(num).equals(field) || num < 0 || num > 255) {
41                 return false;
42             }
43         }
44         return true;
45     }
46     
47     private boolean isIPv6(String ip) {
48         // special case
49         if (ip.charAt(ip.length() - 1) == ':') {
50             return false;
51         }
52         String[] fields = ip.split(":");
53         if (fields.length != 8) {
54             return false;
55         }
56         for (String field : fields) {
57             if (field.length() == 0 || field.length() > 4) {
58                 return false;
59             }
60             for (int i = 0; i < field.length(); i++) {
61                 char c = field.charAt(i);
62                 if (!((c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F') || (c >= '0' && c <= '9'))) {
63                     return false;
64                 }
65             }
66         }
67         return true;
68     }
69 }

 

LeetCode 题目总结

posted @ 2020-06-17 01:56  CNoodle  阅读(294)  评论(0编辑  收藏  举报