题解 | #合法IP#
合法IP
http://www.nowcoder.com/practice/995b8a548827494699dc38c3e2a54ee9
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
String[] str = sc.nextLine().split("\\.");
boolean flag = true;
if(str.length == 4){
for(int i = 0; i < 4; i++){
if(str[i].equals("")){
flag = false;
break;
}else if(str[i].length()>1 && str[i].charAt(0)=='0'){
flag = false;
break;
}else if(str[i].replaceAll("[0-9]","").length() != 0){
flag = false;
break;
}else if(Integer.parseInt(str[i])<0 || Integer.parseInt(str[i])>255){
flag = false;
break;
}else{
flag = true;
}
}
}else{
flag = false;
}
if(flag == true){
System.out.println("YES");
}else{
System.out.println("NO");
}
}
}
}