题解 | #合法IP#

合法IP

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

public class Main {

    public static void main(String[] args) {
        java.util.Scanner sc = new java.util.Scanner(System.in);
        String[] str = sc.nextLine().split("\\.");
        boolean flag  = false;
        int count =0;
        if(str.length == 4){
            for(int i=0;i<str.length;i++){
                if(str[i].length() == 0){
                    break;
                }
                if(str[i].replaceAll("[0-9]","").length() !=0){
                    break;
                }
                if(str[i].length() > 1 && str[i].startsWith("0")){
                    break;
                }
                if(Integer.parseInt(str[i]) >=0 && Integer.parseInt(str[i]) <=255){
                  count++;
                }
            }
            if(count == str.length){
                flag = true;
            }
        }
        System.out.println(flag?"YES":"NO");
    }
}

全部评论

相关推荐

零OFFER战士:另一个版本查看图片
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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