题解 | #合法IP#

合法IP

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


public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNextLine()) {
            String n = sc.nextLine();
            String[] s = n.split("\\.");
            if (s.length != 4) {
                System.out.println("NO");
            } else {
                String res = getRes(s);
                System.out.println(res);
            }
        }
    }

    private static String getRes(String[] s) {
        for (String item : s) {
            if ("".equals(item)) {
                return "NO";
            }
            for (int j = 0; j < item.length(); j++) {
                if (item.length() > 1 && item.charAt(0) == '0') {
                    return "NO";
                }
                if (!Character.isDigit(item.charAt(j))) {
                    return "NO";
                }
            }
        }
        for (String value : s) {
            int num = Integer.parseInt(value);
            if (num < 0 || num > 255) {
                return "NO";
            }
        }
        return "YES";
    }
}
全部评论

相关推荐

不愿透露姓名的神秘牛友
07-08 11:16
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
昨天 11:30
点赞 评论 收藏
分享
头顶尖尖的程序员:我是26届的不太懂,25届不应该是找的正式工作吗?为什么还在找实习?大四还实习的话是为了能转正的的岗位吗
点赞 评论 收藏
分享
07-07 14:30
复旦大学 Java
遇到这种人我也不知道说啥了
无能的丈夫:但我觉得这个hr语气没什么问题啊(没有恶意
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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