同学,你的第二题是IP地址码? 我记得是LeetCode 的原题,以前刷过 import java.util.Scanner; public class Main1 {     public static void main(String[] args) {         // TODO Auto-generated method stub         System.out.println("hello world");         Scanner in = new Scanner(System.in);                  String s=in.nextLine();                  vavidIP(s);     }     private static void vavidIP(String s) {         // TODO Auto-generated method stub         int len = s.length();         for (int i = 1; i <=3; ++i){  // first cut             if (len-i > 9) continue;                         for (int j = i+1; j<=i+3; ++j){  //second cut                 if (len-j > 6) continue;                                 for (int k = j+1; k<=j+3 && k<len; ++k){  // third cut                     int a,b,c,d;                // the four int's seperated by "."                     a = Integer.parseInt(s.substring(0,i));                       b = Integer.parseInt(s.substring(i,j)); // notice that "01" can be parsed into 1. Need to deal with that later.                     c = Integer.parseInt(s.substring(j,k));                     d = Integer.parseInt(s.substring(k));                     if (a>255 || b>255 || c>255 || d>255) continue;                      String ip = a+"."+b+"."+c+"."+d;                     if (ip.length()<len+3) continue;  // this is to reject those int's parsed from "01" or "00"-like substrings                     System.out.println(ip);                 }             }         }              }               public static boolean isValid(String s){         if(s.length()>3 || s.length()==0 || (s.charAt(0)=='0' && s.length()>1) || Integer.parseInt(s)>255)             return false;         return true;     } }
点赞 1

相关推荐

点赞 评论 收藏
分享
像好涩一样好学:这公司我也拿过 基本明确周六加班 工资还凑活 另外下次镜头往上点儿
点赞 评论 收藏
分享
点赞 评论 收藏
分享
牛客网
牛客企业服务