关注
同学,你的第二题是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
相关推荐
03-19 10:36
云南大学 C++ 点赞 评论 收藏
分享
牛客热帖
更多
正在热议
更多
# 烂工作和没工作哪个更痛苦? #
3610次浏览 83人参与
# 牛油的搬砖plog #
189689次浏览 1279人参与
# 厦门银行科技岗值不值得投 #
16788次浏览 404人参与
# AI替代不了什么? #
3938次浏览 60人参与
# 发工资后,你做的第一件事是什么 #
100669次浏览 342人参与
# 给工作过的公司写一条大众点评,你会怎么写? #
1797次浏览 30人参与
# 春招至今,你收到几个面试了? #
6735次浏览 95人参与
# 学历VS实习,哪个更重要? #
13411次浏览 195人参与
# 实习心态崩了 #
111352次浏览 566人参与
# 一人分享一道面试手撕题 #
115120次浏览 2929人参与
# OPPO笔试 #
23355次浏览 104人参与
# 谈薪时HR压价该怎么应对 #
294323次浏览 3362人参与
# 工作上你捅过哪些篓子? #
69415次浏览 337人参与
# 产品人求职现状 #
361795次浏览 2604人参与
# 机械校招之路总结 #
120394次浏览 2084人参与
# 面试紧张时你会有什么表现? #
36100次浏览 246人参与
# 你的实习什么时候入职 #
368518次浏览 2372人参与
# uu们,春招你还来吗? #
71148次浏览 956人参与
# 面试中,你被问过哪些奇葩问题? #
100147次浏览 1453人参与
# 刚工作的你,踩过哪些坑? #
33712次浏览 278人参与
# 牛友的志愿填报指南 #
64108次浏览 494人参与
查看5道真题和解析