关注
同学,你的第二题是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
相关推荐
牛客热帖
更多
正在热议
更多
# 在大厂上班是一种什么样的体验 #
11436次浏览 147人参与
# 你的mentor是什么样的人? #
50950次浏览 717人参与
# 程序员找工作至少要刷多少题? #
19861次浏览 255人参与
# 我和mentor的爱恨情仇 #
106083次浏览 943人参与
# 论秋招对个人心气的改变 #
12127次浏览 168人参与
# 校招第一份工作你干了多久? #
136563次浏览 597人参与
# 机械人避雷的岗位/公司 #
43775次浏览 303人参与
# 为了减少AI幻觉,你注入过哪些设定? #
5285次浏览 162人参与
# 设计人如何选offer #
189577次浏览 868人参与
# 你的秋招进行到哪一步了 #
2530215次浏览 23253人参与
# 机械人还在等华为开奖吗? #
312045次浏览 1582人参与
# 牛客AI体验站 #
7204次浏览 197人参与
# 重来一次,我还会选择这个专业吗 #
411137次浏览 3898人参与
# 我现在比当时_,你想录用我吗 #
9080次浏览 116人参与
# 12306一秒售罄,你抢到回家的票了吗? #
2139次浏览 50人参与
# 一张图晒一下你的AI员工 #
5427次浏览 121人参与
# 研究所VS国企,该如何选 #
244430次浏览 1982人参与
# AI Coding的使用心得 #
4910次浏览 105人参与
# 刚入职的你踩过哪些坑 #
7200次浏览 130人参与
# 关于春招/暑期实习,你想知道哪些信息? #
7885次浏览 121人参与