题解 | #合法IP#
合法IP
https://www.nowcoder.com/practice/995b8a548827494699dc38c3e2a54ee9
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String str = scanner.nextLine();
String[] ips = str.split("\\.");
if (ips.length<4||ips.length>4){
System.out.println("NO");
return;
}
for (String ip : ips) {
if(!check(ip)){
System.out.println("NO");
return;
}
}
System.out.println("YES");
}
public static boolean check(String ip){
char[] chars = ip.toCharArray();
for (char aChar : chars) {
if(!Character.isDigit(aChar)){
return false;
}
}
if(ip.startsWith("0")&&!ip.equals("0")){
return false;
}
if("".equals(ip)){
return false;
}
if(Integer.parseInt(ip)>255){
return false;
}
return true;
}
}
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String str = scanner.nextLine();
String[] ips = str.split("\\.");
if (ips.length<4||ips.length>4){
System.out.println("NO");
return;
}
for (String ip : ips) {
if(!check(ip)){
System.out.println("NO");
return;
}
}
System.out.println("YES");
}
public static boolean check(String ip){
char[] chars = ip.toCharArray();
for (char aChar : chars) {
if(!Character.isDigit(aChar)){
return false;
}
}
if(ip.startsWith("0")&&!ip.equals("0")){
return false;
}
if("".equals(ip)){
return false;
}
if(Integer.parseInt(ip)>255){
return false;
}
return true;
}
}