题解 | #合法IP#
合法IP
http://www.nowcoder.com/practice/995b8a548827494699dc38c3e2a54ee9
import java.util.*;
public class Main {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
while(in.hasNext()){
String str = in.next();
String[] nums = str.split("\\.",-1);
String result = "YES";
if(nums.length == 4){
for(String element : nums){
if(element.length() == 0 || element.length() > 3){
result = "NO";
break;
}
for(Character ch : element.toCharArray()){
if(!Character.isDigit(ch)){
result = "NO";
break;
}
}
if(element.charAt(0) == '0' && element.length()!=1){
result = "NO";
break;
}
if(Integer.parseInt(element) > 255){
result = "NO";
break;
}
}
}else{
result = "NO";
}
System.out.println(result);
}
}
}
public class Main {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
while(in.hasNext()){
String str = in.next();
String[] nums = str.split("\\.",-1);
String result = "YES";
if(nums.length == 4){
for(String element : nums){
if(element.length() == 0 || element.length() > 3){
result = "NO";
break;
}
for(Character ch : element.toCharArray()){
if(!Character.isDigit(ch)){
result = "NO";
break;
}
}
if(element.charAt(0) == '0' && element.length()!=1){
result = "NO";
break;
}
if(Integer.parseInt(element) > 255){
result = "NO";
break;
}
}
}else{
result = "NO";
}
System.out.println(result);
}
}
}