关注
同学,你的第二题是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
相关推荐
点赞 评论 收藏
分享
06-16 19:09
河南开封科技传媒学院 Java 
点赞 评论 收藏
分享
牛客热帖
更多
正在热议
更多
# 26届校招投递进展 #
30332次浏览 233人参与
# 小米提前批笔试难吗 #
33997次浏览 357人参与
# 现代汽车前瞻技术研发急速编程挑战赛 #
11726次浏览 126人参与
# 为了找工作你花了哪些钱? #
27722次浏览 262人参与
# 央国企投递记录 #
88035次浏览 1359人参与
# 神州信息工作体验 #
11576次浏览 56人参与
# 你觉得专业和学校哪个对薪资影响最大 #
61331次浏览 490人参与
# 设计人的面试记录 #
123380次浏览 1341人参与
# 来聊聊你目前的求职进展 #
634227次浏览 6745人参与
# 外包能不能当跳板? #
34379次浏览 220人参与
# 你今年的保底offer是哪家 #
118348次浏览 537人参与
# 烟草笔面经互助 #
16893次浏览 180人参与
# 大疆的机械笔试比去年难吗 #
72892次浏览 618人参与
# 打工人的精神状态 #
49534次浏览 858人参与
# 牛友们,签完三方你在忙什么? #
98220次浏览 852人参与
# 听到哪句话就代表面试稳了or挂了? #
170762次浏览 1369人参与
# 如何缓解入职前的焦虑 #
192364次浏览 1339人参与
# 研究所VS国企,该如何选 #
184846次浏览 1783人参与
# 你秋招想去哪些公司 #
22196次浏览 809人参与
# 担心入职之后被发现很菜怎么办 #
130800次浏览 775人参与
# 秋招结束之后的日子 #
75200次浏览 911人参与