题解 | #合法IP#
合法IP
https://www.nowcoder.com/practice/995b8a548827494699dc38c3e2a54ee9
import java.util.ArrayList; import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 while (in.hasNext()) { // 注意 while 处理多个 case String line = in.next(); String[] ss = line.split("\\."); ArrayList<String> list = new ArrayList<>(); int count = 0; for(String s: ss){ if(s.isEmpty()){ System.out.print("NO"); return; } if(s.charAt(0) == '0' && s.length() != 1){ System.out.print("NO"); return; } for(Character c: s.toCharArray()){ if(!Character.isDigit(c)){ System.out.print("NO"); return; } } int t = Integer.parseInt(s); if(t > 255 || t < 0) { System.out.print("NO"); return; } count++; } if(count != 4){ System.out.print("NO"); return; } System.out.print("YES"); } } }