题解 | #合法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");
        }



    }
}

全部评论

相关推荐

Jing_Rainb...:小公司搞出大厂流程,要么是真缺技术大牛,要么是老板的偶像包袱😂…面完记得来更新后续!
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务