关注
同学,你的第二题是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
相关推荐
点赞 评论 收藏
分享
牛客热帖
更多
正在热议
更多
# 牛油的搬砖plog #
23382次浏览 104人参与
# 运营每日一题 #
67273次浏览 643人参与
# 一人一个landing小技巧 #
21576次浏览 421人参与
# 找工作的破防时刻 #
23672次浏览 371人参与
# 我在牛爱网找对象 #
177636次浏览 1335人参与
# 为什么那么多公司毁约 #
163030次浏览 1221人参与
# 520告白墙 #
16038次浏览 283人参与
# 实习学不到东西怎么办? #
202988次浏览 2111人参与
# 2023届毁约公司名单 #
190123次浏览 938人参与
# 实习/项目/竞赛奖项,哪个对找工作更重要? #
80063次浏览 1102人参与
# 腾讯音乐求职进展汇总 #
85977次浏览 482人参与
# 25届秋招公司红黑榜 #
259231次浏览 1089人参与
# 面试被问第一学历差时该怎么回答 #
121960次浏览 762人参与
# 电网笔面经互助 #
31396次浏览 317人参与
# 我想象的实习vs现实的实习 #
277269次浏览 2207人参与
# 被同事甩锅了怎么办 #
18886次浏览 94人参与
# 哪些企业的面试体验感最差? #
23884次浏览 233人参与
# 我发现一个规律 #
2544次浏览 26人参与
# 工作中,你有没有遇到非常爱骂人的领导? #
28856次浏览 155人参与
# 拼多多工作体验 #
22349次浏览 156人参与