题解 | #合法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 scanner = new Scanner(System.in); String str = scanner.nextLine(); String[] res = str.split("\\."); boolean ifValidation = true; if (res.length != 4) { ifValidation = false; } else { for (String re : res) { if (re.matches("^\\D.*") || re.length() == 0 || (re.startsWith("0") && re.length() > 1)) { ifValidation = false; break; } int num = Integer.parseInt(re); if (num < 0 || num > 255) { ifValidation = false; break; } } } if (ifValidation) System.out.println("YES"); else System.out.println("NO"); } }