IPV4地址可以用一个32位无符号整数来表示,一般用点分方式来显示,点将IP地址分成4个部分,每个部分为8位,表示成一个无符号整数(因此正号不需要出现),如10.137.17.1,是我们非常熟悉的IP地址,一个IP地址串中没有空格出现(因为要表示成一个32数字)。
现在需要你用程序来判断IP是否合法。
数据范围:数据组数:
进阶:时间复杂度:
,空间复杂度:%5C)
IPV4地址可以用一个32位无符号整数来表示,一般用点分方式来显示,点将IP地址分成4个部分,每个部分为8位,表示成一个无符号整数(因此正号不需要出现),如10.137.17.1,是我们非常熟悉的IP地址,一个IP地址串中没有空格出现(因为要表示成一个32数字)。
输入一个ip地址,保证不包含空格
返回判断的结果YES or NO
255.255.255.1000
NO
java版本:解决了热门代码中无法判断12*.21.42.1之类的带有非法字符*的问题
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNextLine()) {
//合格的IPV4地址:①4个. ②每个点数介于0~255 ③不得以0开头,且只有一个零。④每段中不能包含非数字字符,比如- + * u 等等
String str = in.nextLine();
String[] strs = str.split("\\.");
if (strs.length != 4){
System.out.println("NO");
continue;
}else if (!numsSizeIsValid(strs)){
System.out.println("NO");
continue;
}else if (!startWithNoZero(strs)){
System.out.println("NO");
continue;
}
System.out.println("YES");
}
}
private static boolean numsSizeIsValid(String[] strs){
for (String s : strs){
for (int c : s.toCharArray()){
if (!Character.isDigit(c)){
return false;
}
}
if (s.length() == 0 || Integer.valueOf(s) < 0 || Integer.valueOf(s) > 255){
return false;
}
}
return true;
}
private static boolean startWithNoZero(String[] strs){
for (String s : strs){
if (s.startsWith("0") && s.length() > 1){
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().split("\\.");
System.out.println(check(a));
}
}
public static String check(String[] a){
if(a.length!=4){
return "NO";
}
boolean flag=true;
for(int i=0;i<a.length;i++){
// .1.3.8
if(a[i]==null||a[i].equals("")){
flag = false;
break;
}
int k=Integer.parseInt(a[i]);
// 01 ->1
if((""+k).length()!=(a[i].length())){
flag = false;
break;
}
//>255&nbs***bsp;<0
if(k<0||k>255){
flag = false;
break;
}
}
return flag?"YES":"NO";
}
} import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String input = sc.nextLine().trim();
// 改进后的正则表达式
String regex =
"^((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\\.){3}(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])$";
if (input.matches(regex)) {
System.out.println("YES");
} else {
System.out.println("NO");
}
}
} import java.io.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
String s = bf.readLine();
String regex0 = "(\\d|[1-9]\\d|1\\d\\d|2[0-4]\\d|25[0-5])";
String regexIPv4 = regex0 + "(\\." + regex0 + "){3}";
boolean b = s.matches(regexIPv4);
System.out.println(b?"YES":"NO");
}
} import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String strIp = sc.nextLine();
String[] ips = strIp.split("\\.");
if (ips.length != 4) {
System.out.println("NO");
return;
} else {
for (String ip : ips) {
if (ip == null || ip.equals("")) {
System.out.println("NO");
return;
}
int num = Integer.parseInt(ip);
if (num < 0 || num > 255) {
System.out.println("NO");
return;
} else if (ip.length() != String.valueOf(num).length()) {
System.out.println("NO");
return;
}
}
}
System.out.println("YES");
}
} 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");
}
}