题解 | #合法IP#

合法IP

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

import java.util.ArrayList;
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.hasNext()) { // 注意 while 处理多个 case
            String line = in.next();
            String[] ss = line.split("\\.");
            ArrayList<String> list = new ArrayList<>();
            int count = 0;
            for(String s: ss){
                if(s.isEmpty()){
                    System.out.print("NO");
                    return;
                }
                if(s.charAt(0) == '0' && s.length() != 1){
                    System.out.print("NO");
                    return;
                }
                for(Character c: s.toCharArray()){
                    if(!Character.isDigit(c)){
                        System.out.print("NO");
                        return;
                    }
                }
                int t = Integer.parseInt(s);
                if(t > 255 || t < 0) {
                    System.out.print("NO");
                    return;
                }
                count++;
            }
            if(count != 4){
                    System.out.print("NO");
                    return;
                }
            System.out.print("YES");
        }
    }
}

全部评论

相关推荐

01-05 09:14
同济大学 Java
心碎一号线:我要是9✌🏻我就选保研,保研了大四再找实习,实习之后,如果觉得自己不适合互联网工作模式,还能有其他选择,如果实习后决定了走互联网,也能提升学历提高竞争力
点赞 评论 收藏
分享
02-16 01:39
南昌大学 Java
坚持无悔意无休:xhs上集美最爱说谎博人眼球
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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