IPV4地址可以用一个32位无符号整数来表示,一般用点分方式来显示,点将IP地址分成4个部分,每个部分为8位,表示成一个无符号整数(因此正号不需要出现),如10.137.17.1,是我们非常熟悉的IP地址,一个IP地址串中没有空格出现(因为要表示成一个32数字)。
现在需要你用程序来判断IP是否合法。
数据范围:数据组数:
进阶:时间复杂度:,空间复杂度:
IPV4地址可以用一个32位无符号整数来表示,一般用点分方式来显示,点将IP地址分成4个部分,每个部分为8位,表示成一个无符号整数(因此正号不需要出现),如10.137.17.1,是我们非常熟悉的IP地址,一个IP地址串中没有空格出现(因为要表示成一个32数字)。
输入一个ip地址,保证不包含空格
返回判断的结果YES or NO
255.255.255.1000
NO
import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String s1 = in.nextLine(); String result = "YES"; String s2=""; try { int[] ip = Arrays.stream(s1.split("\\.")).mapToInt(Integer::parseInt).toArray(); for (int i = 0; i < ip.length; i++) { s2=s2+ip[i]+"."; if (!(ip[i] >= 0 && ip[i] <= 255)) { result = "NO"; } } if (ip.length != 4) { result = "NO"; } if(!s2.substring(0, s2.length()-1).equals(s1)){ result = "NO"; } }catch (Exception e){ result = "NO"; } System.out.println(result); } }
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String [] arr = in.nextLine().split("\\."); String result = "YES"; if (arr.length != 4) { result = "NO"; } for (int i = 0; i < arr.length; i++) { if(arr[i].length() == 0 || !arr[i].matches("(0|[1-9]\\d*)")){ result = "NO"; break; } Integer n = Integer.parseInt(arr[i]); if ( n < 0 || n > 255) { result = "NO"; } } System.out.println(result); } }
public static boolean checkIP(String str) { String[] strs = str.split("\\."); if (strs.length != 4) { return false; } for (String s : strs) { if (!s.matches("^(0|[1-9]\\d*)$")) return false; int val = Integer.valueOf(s); if (val > 255 || val < 0) { return false; } } return true; }
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 while (in.hasNext()) { // 注意 while 处理多个 case String a = in.nextLine(); String[] IPs = a.split("\\."); // 1-保证分成了四部分 if(IPs.length != 4) { System.out.println("NO"); return; } for(int i = 0; i < 4; i++) { // 2-四部分中没有为空的部分 if("".equals(IPs[i]) || IPs[i] == null) { System.out.println("NO"); return; } // 3-不为空要保证每个字符都是数字组成的 for(int j = 0; j < IPs[i].length(); j++) { if(IPs[i].charAt(j) < '0' || IPs[i].charAt(j) > '9') { System.out.println("NO"); return; } } int ip = Integer.parseInt(IPs[i]); // 4-数字在不为0的时候不能用0开头 if('0' == IPs[i].charAt(0) && ip != 0) { System.out.println("NO"); return; } // 5-保证合法的数字范围 if(ip < 0 || ip > 255) { System.out.println("NO"); return; } } System.out.println("YES"); } } }
import java.util.Scanner; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String line; while ((line = br.readLine()) != null) { System.out.println(checkIP(line));; } } /** * 判断ip地址是否合法 * @param line * @return */ private static String checkIP(String line) { String[] split = line.split("\\."); // 长度不为4的不合法 if (split.length != 4) { return "NO"; } for (int i = 0; i < split.length; i++) { try { int num = Integer.parseInt(split[i]); // 大于255的不合法 if (num > 255) { return "NO"; } // 如果以0开头,但不等于0的,不合法 if (split[i].startsWith("0") && num != 0){ return "NO"; } // 带符号的不合法 if (split[i].startsWith("+") || split[i].startsWith("-")){ return "NO"; } } catch (NumberFormatException e) { // 转换为数字异常的不合法 return "NO"; } } return "YES"; } }
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { String[] strs = sc.nextLine().split("\\."); String jg="YES"; if(strs.length==4){ for(String s:strs){ if(s.length()==0){ jg="NO"; break; } for(Character ch:s.toCharArray()){ if(!Character.isDigit(ch)){ jg="NO"; break; } } if(s.length()!=1&&s.charAt(0)=='0'){ jg="NO"; break; } if(Integer.parseInt(s)>255){ jg="NO"; break; } } }else{ jg="NO"; } System.out.println(jg); } } }
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 while (in.hasNext()) { // 注意 while 处理多个 case boolean flag = true; String[] parts = in.next().split("\\."); if (parts.length != 4) { flag = false; } else { for (String s : parts) { if ("".equals(s) || s.matches("^[+-].*") || s.matches("^0\\d+") || Integer.parseInt(s) > 255) { flag = false; break; } } } System.out.println(flag ? "YES" : "NO"); } } }
public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String str = br.readLine(); String[] strs = str.split("\\."); String result = "YES"; if( strs.length == 4) { for (int i = 0; i < strs.length; i++) { String s = strs[i]; //确保每个位置上有值 if (s.length()<1) { result = "NO";break; } //确保每个段位是数字类型 if (!isNumber(s)) { result = "NO";break; } //确保每个段位的值不大于255 if (Integer.parseInt(s) > 255) { result = "NO";break; } //确保每个段位两位数以上时,不是以0开头 if (s.length() > 1 && s.startsWith("0")) { result = "NO";break; } } }else { result = "NO"; } System.out.println(result); } private static boolean isNumber(String s) { for (int i = 0; i < s.length(); i++) { if (!Character.isDigit(s.charAt(i))) { return false; } } return true; }
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String line = null; while((line = br.readLine()) != null){ System.out.println(isValidate(line)); } } public static String isValidate(String line){ String[] IPArr = line.split("\\."); int temp = 0; if(IPArr.length != 4){ // IPV4必须分为4个数组 return "NO"; } for(int i = 0; i < 4; i++){ if(IPArr[i] == null || IPArr[i].length() == 0){ // 分组后的每一个数不能为null或者"" return "NO"; } temp = Integer.parseInt(IPArr[i]); if(!IPArr[i].equals(temp + "")){ // 排除以0开头的数 return "NO"; } if(temp < 0 || temp > 255){ return "NO"; } } return "YES"; } }
public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String ip = br.readLine(); int len = ip.length(); int counts = 0, cur = 0; boolean isValid = true, isEmpty = true; for (int i = 0; i < len; i++) { char ch = ip.charAt(i); if (ch == '.') { if (isEmpty || i == len - 1) { isValid = false; break; } isEmpty = true; counts++; cur = 0; continue; } if (!Character.isDigit(ch)) { isValid = false; break; } int num = ch - '0'; if (cur == 0 && num == 0 && i != len - 1 && ip.charAt(i + 1) != '.') { isValid = false; break; } isEmpty = false; cur = cur * 10 + num; if (cur > 255) { isValid = false; break; } } isValid = counts == 3 && isValid; System.out.println(isValid ? "YES" : "NO"); } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String str = scanner.nextLine(); if (str.contains("+")||str.contains("-")){ System.out.println("NO"); return; } String[] strArray = str.split("\\."); for (String s : strArray) { if (s.equals("") || (s.charAt(0) == '0' && s.length()>1) || Integer.parseInt(s) > 255 || strArray.length != 4 || Integer.parseInt(s) < 0) { System.out.println("NO"); return; } } System.out.println("YES"); } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String str = scanner.nextLine(); String[] strArray = str.split("\\."); if (str.contains("+")||str.contains("-")){ System.out.println("NO"); return; } for (String s : strArray) { if (s.equals("") || (s.charAt(0) == '0'&&s.length()>1) || Integer.parseInt(s) > 255 || strArray.length != 4 || Integer.parseInt(s) < 0) { System.out.println("NO"); return; } } System.out.println("YES"); } }
public static void main(String[] args) { Scanner sc = new Scanner(System.in); String line = sc.nextLine(); String[] split = line.split("\\."); //必须是四部分 if (split.length != 4){ System.out.println("NO"); return; } boolean legal = true; for (int i = 0; i < split.length; i++) { String numstr = split[i]; //每部分不能为空 if (numstr == null || "".equals(numstr)){ legal = false; break; } //每部分必须全是数字 char[] chars = numstr.toCharArray(); boolean numic = true; for (int j = 0; j < chars.length; j++) { if (!getNumic(chars[j])) { numic = false; break; } } if (!numic) { legal = false; break; } //数字不能太长,太长转换int类型会报错 if (numstr.length() > 3) { legal = false; break; } //数字不能超过255 Integer num = Integer.valueOf(numstr); if (num > 255) { legal = false; break; } //首位是否是有效数字,针对 099.255.255.255 String strcopy = String.valueOf(num); if (!strcopy.equals(numstr)){ legal = false; break; } } System.out.println(legal ? "YES" : "NO"); } //校验是否是数字 public static boolean getNumic(char c) { if (c >= '0' && c <= '9') { return true; } else { return false; } }
import java.util.*; public class Main{ public static boolean isTrue(String[] strs){ if(strs.length != 4){ return false; } for(int i=0;i<strs.length;i++){ for(int j=0;j<strs[i].length();j++){ if(!Character.isDigit(strs[i].charAt(j))){ return false; } } if(strs[i]==null || strs[i].length()==0){ return false; } if(strs[i].startsWith("0") && Integer.valueOf(strs[i])!=0){ return false; } if(Integer.valueOf(strs[i])<0 || Integer.valueOf(strs[i])>255){ return false; } } return true; } public static void main(String[] args){ Scanner sc = new Scanner(System.in); while(sc.hasNext()){ String str = sc.next(); String[] strs = str.split("\\."); String res = isTrue(strs)? "YES":"NO"; System.out.println(res); } } }