题解 | #合法IP#
合法IP
http://www.nowcoder.com/practice/995b8a548827494699dc38c3e2a54ee9
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String args[]) {
Scanner scan = new Scanner(System.in);
while (scan.hasNext()) {
String ip = scan.nextLine();
String [] iparray = ip.split("\\.");////\\符号
boolean flag = true;
boolean flag2 = false;
if (iparray.length == 4 ) {
for (int i = 0; i < iparray.length; i++) {
if (iparray[i].equals("")) {////equals中有s,字符串可用equals,char没有equals函数
flag = false;
break;
} else {
int temp = Integer.parseInt(iparray[i]);////parse语法解析的意思
if (temp < 0 || temp >= 256) {
flag = false;
break;
}
char [] tempc = iparray[i].toCharArray();
for (int j = 0; j < tempc.length; j++) {
String temp3 = "" + tempc[j];
if (temp3.equals("+")) {
flag = false;
break;
}
}
if (tempc.length >= 2 && !("" + tempc[0]).equals("+") &&
Integer.parseInt("" + tempc[0]) == 0) {
flag = false;
break;
}
}
}
} else {
flag = false;
}
if (flag) {
System.out.println("YES");
} else {
System.out.println("NO");
}
}
}
}