LeetCode 468. 【Java】468. Validate IP Address
原题链接
中等
作者:
tt2767
,
2020-03-06 22:20:33
,
所有人可见
,
阅读 1186
/*
1. 合法的IPv4:
1.1 .分割为4组
1.2 每组不为空
1.3 每组长度 <= 3
1.4 每组在0~255之间
1.5 每组不包含前导0, 除非本身为0
2. 合法的IPv6:
2.1 : 分割为8组
2.2 每组不为空
2.3 每组长度 <=4
2.4 每组为一个16进制数: 即字符范围为: 0~9, a~f, A~F
*/
class Solution {
public String validIPAddress(String IP) {
if (IP == null || IP.length() == 0) return "Neither";
if (isIPv4(IP)) return "IPv4";
if (isIPv6(IP)) return "IPv6";
return "Neither";
}
public boolean isIPv4(String IP){
String[] ips = IP.split("\\.");
if (ips.length != 4) return false;
if (IP.charAt(0) == '.' || IP.charAt(IP.length()-1) == '.') return false;
for (int i = 0 ; i < ips.length ; i ++)
if (!isIPv4Group(ips[i])) return false;
return true;
}
private boolean isIPv4Group(String IPG){
if (IPG == null || IPG.length() == 0 || IPG.length() > 3) return false;
for (int i = 0 ; i < IPG.length(); i++)
if (!('0' <= IPG.charAt(i) && IPG.charAt(i) <= '9')) return false;
int x = Integer.valueOf(IPG);
if (x< 0 || x > 255 || (IPG.charAt(0) == '0' && IPG.length() > 1))
return false;
return true;
}
public boolean isIPv6(String IP){
String[] ips = IP.split(":");
if (ips.length != 8) return false;
if (IP.charAt(0) == ':' || IP.charAt(IP.length()-1) == ':') return false;
for (int i = 0 ; i < ips.length ; i ++){
if (!isIPv6Group(ips[i])) return false;
}
return true;
}
private boolean isIPv6Group(String IPG){
if (IPG == null || IPG.length() == 0 || IPG.length() > 4) return false;
for (int i = 0 ; i < IPG.length(); i++)
if (!('0' <= IPG.charAt(i) && IPG.charAt(i) <= '9' || 'a' <= IPG.charAt(i) && IPG.charAt(i) <= 'f' || 'A' <= IPG.charAt(i) && IPG.charAt(i) <='F'))
return false;
return true;
}
}