题解 | #判断两个IP是否属于同一子网#

判断两个IP是否属于同一子网

https://www.nowcoder.com/practice/34a597ee15eb4fa2b956f4c595f03218

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.function.Function;
import java.util.function.IntFunction;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

import static java.util.Arrays.*;
import static java.util.stream.Stream.*;


public class Main {
    public static void main(String[] args) throws IOException {

        testTh();
    }

    private static void testTh() throws IOException {
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        String str;

        while ((str = bf.readLine()) != null) {
            String ip_1 = bf.readLine();
            String ip_2 = bf.readLine();
            if (!isLegalNetmask(str) || !isLegalIp(ip_1) || !isLegalIp(ip_2)) {
                System.out.println(1);
            } else {
                ArrayList<String> netmask = ip2BinArrayList(str);
                ArrayList<String> ip1 = ip2BinArrayList(ip_1);
                ArrayList<String> ip2 = ip2BinArrayList(ip_2);

                if (isEqualIpAndNetmask(ip1, ip2, netmask)) {
                    System.out.println(0);
                } else {
                    System.out.println(2);
                }
            }



        }


    }

    public static ArrayList<String> ip2BinArrayList(String ip) {
        ArrayList<String> res = new ArrayList<>();
        String[] netmask = ip.split("\\.");
        for (int i = 0; i < 4; i++) {
            String temp = Integer.toBinaryString(Integer.parseInt(netmask[i]));
            if (temp.length() != 8) {
                int length = temp.length();
                for (int j = 0; j < 8 - length; j++) {
                    temp = "0" + temp;
                }
            }
            res.add(temp);
        }
        return res;
    }
    public static  Boolean isEqualIpAndNetmask(ArrayList<String> ip1,
            ArrayList<String> ip2, ArrayList<String> netmask) {
        return consultIpAndNetmask(ip1, netmask).equals(consultIpAndNetmask(ip2,
                netmask));
    }
    public static String consultIpAndNetmask(ArrayList<String> ip,
            ArrayList<String> netmask) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 4; i++) {
            String tempIp = ip.get(i);
            String tempNetmask = netmask.get(i);
            for (int j = 0; j < 8; j++) {
                int r = Integer.parseInt(tempIp.charAt(j) + "") + Integer.parseInt(
                            tempNetmask.charAt(j) + "");
                if (r == 1 || r == 0) {
                    sb.append("0");
                } else {
                    sb.append("1");
                }
            }
            sb.append(".");
        }
        return sb.toString();
    }
    public static Boolean isLegalNetmask(String netmask) {
        String[] split = netmask.split("\\.");
        if (split.length != 4) {
            return false;
        }

        if (Integer.parseInt(split[0]) == 0)return false;
        for (int i = 0; i < 4; i++) {
            int parseInt = Integer.parseInt(split[i]);
            if (parseInt<0||parseInt>255)return false;
            if (parseInt != 255 && parseInt != 0) {
                String binaryString = Integer.toBinaryString(parseInt);
                int lastIndexOf = binaryString.lastIndexOf("1");
                int indexOf = binaryString.indexOf("0");
                if (indexOf - lastIndexOf != 1) {
                    return false;
                }
            }
            if (parseInt == 255 && i != 0) {
                int j = i - 1;
                if (Integer.parseInt(split[j]) != 255) {
                    return false;
                }
            }
            if (parseInt == 0 && i != 3) {
                int j = i + 1;
                if (Integer.parseInt(split[j]) != 0) {
                    return false;
                }
            }
        }
        return true;
    }
    public static Boolean isLegalIp(String ip) {
        String[] split = ip.split("\\.");
        for (int i = 0; i < 4; i++) {
            int parseInt = Integer.parseInt(split[i]);
            if (parseInt < 0 || parseInt > 255) {

                return false;
            }
        }
        return true;
    }
}

全部评论

相关推荐

点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务