rambless

合法IP

https://www.nowcoder.com/practice/995b8a548827494699dc38c3e2a54ee9

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.hasNextLine()) { // 注意 while 处理多个 case
            String str = in.nextLine();
            System.out.println(match(str));
        }
    }

    private static String match(String str) {
        String[] arr = str.split("\\.");
        if(arr.length != 4) {
            return "NO";
        }
        for(int i=0; i<arr.length; i++) {
            if(arr[i].length() == 0) {
                return "NO";
            }
            if(arr[i].startsWith("+") || arr[i].startsWith("-")) {
                return "NO";
            }
            if(arr[i].length()>3) {
                return "NO";
            }
            if(arr[i].startsWith("0") && arr[i].length()>1) {
                return "NO";
            }
            if(Integer.parseInt(arr[i])>255) {
                return "NO";
            }
        }
        return "YES";
    }
}

全部评论

相关推荐

合不合适,我自己说了才算
码农索隆:hr:“真执着啊,来我公司当法人吧”
点赞 评论 收藏
分享
07-09 18:33
门头沟学院 Java
这么逆天每年都有人去???&nbsp;填多益网申就是大型的服从性测试
鲁大牛:辅导员在群里发了这个公司我就申了一下。网申居然要写当场开摄像头写两篇不少于三百字的作文。太逆天了
点赞 评论 收藏
分享
求offer的大角牛:不吃香菜
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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