1. 验证IP地址
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/c71869a92372851fe6085a592abf01cb.png)
(1)简单想法
class Solution {
public String validIPAddress(String IP) {
if (IP == null || "".equals(IP)) {
return "Neither";
}
if (IP.contains(".")) {
return ipV4(IP);
} else if(IP.contains(":")) {
return ipV6(IP);
} else {
return "Neither";
}
}
public String ipV4(String IP) {
String[] ipSegments = IP.split("\\.", -1);
if (ipSegments.length != 4) {
return "Neither";
}
for (String s:ipSegments) {
if (s.length() == 0 || s.length() > 3 || (s.charAt(0) == '0' && s.length() > 1)) {
return "Neither";
}
for (int i = 0; i < s.length(); i++) {
if ( !Character.isDigit(s.charAt(i))) {
return "Neither";
}
}
int val = Integer.parseInt(s);
if(val > 255) {
return "Neither";
}
}
return "IPv4";
}
public String ipV6(String IP) {
String[] ipSegments = IP.split(":",-1);
if (ipSegments.length != 8) {
return "Neither";
}
for (String s : ipSegments) {
if (s.length() == 0 || s.length() > 4) {
return "Neither";
}
for (int i=0; i < s.length(); i++) {
char ch = s.charAt(i);
if( !Character.isDigit(ch) && (Character.toLowerCase(ch)>'f' || Character.toLowerCase(ch) < 'a')) {
return "Neither";
}
}
}
return "IPv6";
}
}