系统设计题-路由表最长匹配

一、题目

路由表最长匹配:将目标IP地址dstIP与路由为entryIP/掩码长度m(比如10.166.50.0/23)进行匹配,找出匹配掩码m最长值。
匹配规则
如果dstIP和entryIP的二进制表示的前m个位相同,则说明是匹配的。
0.0.0.0是默认路由,与任何dstIP均匹配,m值为0。
所有匹配的路由中,m的最大值即为“最长匹配”

二、样例

第一行是dstIP
第二行是总entryIP数
第三行之后是具体的entryIP/m

192.168.0.3
6
10.166.50.0/23
192.0.0.0/0
10.255.255.255/32
192.168.0.1/24
127.0.0.0/0
192.168.0.0.0/24
输出
192.168.0.1/24

三、代码实现

    // ipTable是个二维数组,每一个数组中第0列存IP,第1列存掩码
    private static String routeSearch(String dstIp, String[][] ipTable) {
        String dstIpBinary = getRoute(dstIp);
        LinkedList<String> maxMatchList = new LinkedList<>();
        int maxMatch = -1;
        for (String[] strings : ipTable) {
            String tempRoute = strings[0];
            int maxNum = Integer.parseInt(strings[1]);
            if (tempRoute.equals("0.0.0.0") && maxNum == 0) {
                maxMatch = maxNum;
                maxMatchList.add(tempRoute + "/" + maxNum);
                continue;
            }
            if (isMatch(dstIpBinary, getRoute(tempRoute), maxNum) && maxNum > maxMatch) {
                maxMatchList.clear();
                maxMatch = maxNum;
                maxMatchList.add(tempRoute + "/" + maxNum);
            }
        }

        if (maxMatchList.isEmpty()) {
            return "empty";
        }
        return maxMatchList.getFirst();
    }

    // 判断在给定matchNum长度下,是否匹配
    private static boolean isMatch(String src, String ds, int matchNum) {
        for (int i = 0; i < matchNum; i++) {
            if (src.charAt(i) != ds.charAt(i)) {
                return false;
            }
        }
        return true;
    }

    // 将IP地址转换成二进制
    private static String getRoute(String s) {
        String[] ipSplit = s.split("\\.");
        StringBuilder stringBuilder = new StringBuilder();
        for (String ip : ipSplit) {
            // 转换成二进制,不足8位,则补0
            String binaryString = Integer.toBinaryString(Integer.parseInt(ip));
            String formatBinaryStr = String.format("%8s", binaryString);
            formatBinaryStr = formatBinaryStr.replace(" ", "0");
            stringBuilder.append(formatBinaryStr);
        }
        return stringBuilder.toString();
    }
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值