题解 | #合法IP#
合法IP
https://www.nowcoder.com/practice/995b8a548827494699dc38c3e2a54ee9
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[]args) { Scanner input = new Scanner(System.in); String s = input.nextLine(); String[] split = s.split("\\."); if (split.length != 4) { System.out.println("NO"); return; } try { for (String s1 : split) {//超出范围 if (!(Integer.valueOf(s1) >= 0 && Integer.valueOf(s1) <= 255)) { System.out.println("NO"); return; } if (s1.startsWith("0") && s1.length() > 1) {//0开头的多位数字 System.out.println("NO"); return; } if (s1.contains("+") || s1.contains("-")) {//包含正负号 System.out.println("NO"); return; } } System.out.println("YES"); } catch (Exception e) {//异常控制其他情况 System.out.println("NO"); } } }