|
| 1 | +package com.cjl.leetcode; |
| 2 | + |
| 3 | +import java.util.*; |
| 4 | + |
| 5 | +/* |
| 6 | + 468. 验证IP地址 |
| 7 | + 问题描述: |
| 8 | + 给定一个字符串 queryIP。如果是有效的 IPv4 地址,返回 "IPv4" ;如果是有效的 IPv6 地址,返回 "IPv6" ;如果不是上述类型的 IP 地址,返回 "Neither" 。 |
| 9 | + 有效的IPv4地址 是 “x1.x2.x3.x4” 形式的IP地址。 其中 0 <= xi <= 255 且 xi 不能包含 前导零。例如: |
| 10 | + “192.168.1.1” 、 “192.168.1.0” 为有效IPv4地址, “192.168.01.1” 为无效IPv4地址; “192.168.1.00” 、 “[email protected]” 为无效IPv4地址。 |
| 11 | + 一个有效的IPv6地址 是一个格式为“x1:x2:x3:x4:x5:x6:x7:x8” 的IP地址,其中: |
| 12 | + 1 <= xi.length <= 4 |
| 13 | + xi 是一个 十六进制字符串 ,可以包含数字、小写英文字母( 'a' 到 'f' )和大写英文字母( 'A' 到 'F' )。 |
| 14 | + 在 xi 中允许前导零。 |
| 15 | + 例如 "2001:0db8:85a3:0000:0000:8a2e:0370:7334" 和 "2001:db8:85a3:0:0:8A2E:0370:7334" 是有效的 IPv6 地址, |
| 16 | + 而 "2001:0db8:85a3::8A2E:037j:7334" 和 "02001:0db8:85a3:0000:0000:8a2e:0370:7334" 是无效的 IPv6 地址。 |
| 17 | + 示例 1: |
| 18 | + 输入:queryIP = "172.16.254.1" |
| 19 | + 输出:"IPv4" |
| 20 | + 解释:有效的 IPv4 地址,返回 "IPv4" |
| 21 | + 示例 2: |
| 22 | + 输入:queryIP = "2001:0db8:85a3:0:0:8A2E:0370:7334" |
| 23 | + 输出:"IPv6" |
| 24 | + 解释:有效的 IPv6 地址,返回 "IPv6" |
| 25 | + 示例 3: |
| 26 | + 输入:queryIP = "256.256.256.256" |
| 27 | + 输出:"Neither" |
| 28 | + 解释:既不是 IPv4 地址,又不是 IPv6 地址 |
| 29 | + 提示: |
| 30 | + queryIP 仅由英文字母,数字,字符 '.' 和 ':' 组成。 |
| 31 | + */ |
| 32 | +public class Question_468 { |
| 33 | + |
| 34 | + // 遍历对比解法 |
| 35 | + // 时间复杂度是O(N),空间复杂度是O(1) |
| 36 | + public String solution1(String queryIP){ |
| 37 | + if (queryIP.contains(".") && isIPv4(queryIP)) return "IPv4"; |
| 38 | + if (queryIP.contains(":") && isIPv6(queryIP)) return "IPv6"; |
| 39 | + return "Neither"; |
| 40 | + } |
| 41 | + |
| 42 | + // 正则解法 |
| 43 | + public String solution2(String queryIP) { |
| 44 | + String result = "Neither"; |
| 45 | + if (queryIP == null) { |
| 46 | + return result; |
| 47 | + } |
| 48 | + String regex0 = "(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)"; |
| 49 | + String regexIPv4 = regex0 + "(\\." + regex0 + "){3}"; |
| 50 | + String regex1 = "([\\da-fA-F]{1,4})"; |
| 51 | + String regexIPv6 = regex1 + "(:" + regex1 + "){7}"; |
| 52 | + |
| 53 | + if (queryIP.matches(regexIPv4)) { |
| 54 | + result = "IPv4"; |
| 55 | + } else if(queryIP.matches(regexIPv6)) { |
| 56 | + result = "IPv6"; |
| 57 | + } |
| 58 | + return result; |
| 59 | + } |
| 60 | + |
| 61 | + public boolean isIPv4(String s) { |
| 62 | + String[] strs = s.split("\\.",-1); |
| 63 | + if (strs.length != 4) { |
| 64 | + return false; |
| 65 | + } |
| 66 | + for (int i = 0; i < 4; i++) { |
| 67 | + if (strs[i].length() == 0 || (strs[i].length() > 1 && strs[i].charAt(0) == '0')) { |
| 68 | + return false; |
| 69 | + } |
| 70 | + try { |
| 71 | + int tmpNum = Integer.parseInt(strs[i]); |
| 72 | + if (tmpNum > 255) { |
| 73 | + return false; |
| 74 | + } |
| 75 | + } catch (Exception e) { |
| 76 | + return false; |
| 77 | + } |
| 78 | + } |
| 79 | + return true; |
| 80 | + } |
| 81 | + |
| 82 | + public boolean isIPv6(String s) { |
| 83 | + String[] strs = s.split(":",-1); |
| 84 | + if (strs.length != 8) { |
| 85 | + return false; |
| 86 | + } |
| 87 | + for (int i = 0; i < 8; i++) { |
| 88 | + if (strs[i].length() == 0 || strs[i].length() > 4) { |
| 89 | + return false; |
| 90 | + } |
| 91 | + for (int j = 0; j < strs[i].length(); j++) { |
| 92 | + if (!(strs[i].charAt(j) >= '0' && strs[i].charAt(j) < '9') && |
| 93 | + !(strs[i].charAt(j) >= 'a' && strs[i].charAt(j) <= 'f') && |
| 94 | + !(strs[i].charAt(j) >= 'A' && strs[i].charAt(j) <= 'F')) { |
| 95 | + return false; |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + return true; |
| 100 | + } |
| 101 | +} |
0 commit comments