leetcode-751. IP to CIDR

来源:互联网 发布:2017年nba新秀数据排名 编辑:程序博客网 时间:2024/05/22 09:06

751IP to CIDR

Given a start IP address ip and a number of ips we need to cover n, return a representation of the range as a list (of smallest possible length) of CIDR blocks.

A CIDR block is a string consisting of an IP, followed by a slash, and then the prefix length. For example: "123.45.67.89/20". That prefix length "20" represents the number of common prefix bits in the specified range.

Example 1:

Input: ip = "255.0.0.7", n = 10Output: ["255.0.0.7/32","255.0.0.8/29","255.0.0.16/32"]Explanation:The initial ip address, when converted to binary, looks like this (spaces added for clarity):255.0.0.7 -> 11111111 00000000 00000000 00000111The address "255.0.0.7/32" specifies all addresses with a common prefix of 32 bits to the given address,ie. just this one address.The address "255.0.0.8/29" specifies all addresses with a common prefix of 29 bits to the given address:255.0.0.8 -> 11111111 00000000 00000000 00001000Addresses with common prefix of 29 bits are:11111111 00000000 00000000 0000100011111111 00000000 00000000 0000100111111111 00000000 00000000 0000101011111111 00000000 00000000 0000101111111111 00000000 00000000 0000110011111111 00000000 00000000 0000110111111111 00000000 00000000 0000111011111111 00000000 00000000 00001111The address "255.0.0.16/32" specifies all addresses with a common prefix of 32 bits to the given address,ie. just 11111111 00000000 00000000 00010000.In total, the answer specifies the range of 10 ips starting with the address 255.0.0.7 .There were other representations, such as:["255.0.0.7/32","255.0.0.8/30", "255.0.0.12/30", "255.0.0.16/32"],but our answer was the shortest possible.Also note that a representation beginning with say, "255.0.0.7/30" would be incorrect,because it includes addresses like 255.0.0.4 = 11111111 00000000 00000000 00000100 that are outside the specified range.

Note:

  1. ip will be a valid IPv4 address.
  2. Every implied address ip + x (for x < n) will be a valid IPv4 address.
  3. n will be an integer in the range [1, 1000].

此题做于 leetcode Weekly Contest 64

题意:

题目有点绕,给定一个起始ip地址,一个范围n。返回一个列表表示的最小ip地址块,包含以给定的起始ip为起点的,n个ip地址的ip。例:

Input: ip = "255.0.0.7", n = 10Output: ["255.0.0.7/32","255.0.0.8/29","255.0.0.16/32"]//255.0.0.7/32表示1个IP地址//255.0.0.8/29表示8个IP地址//255.0.0.16/32表示1个IP地址


思路:

本题在做weekly contest时,并没有做出来,以下代码也是别人的,我看过之后,写了自己的注释。 
有一些建议: 
做本题需要一些IP地址的知识,还要理解 16 行的x&-x 操作的含义。

!最好自己先思考一下,对基本的可能出现的情况有一个了解。 
比如IP地址的最后一块是奇数/偶数的情况;IP地址块扩大时能容纳的数量和n的关系等。

class Solution {    public  List<String> ipToCIDR(java.lang.String ip, int range) {        long x = 0;        //获得一个ip地址每一部分        String[] ips = ip.split("\\.");        //将整ip地址看为一个整体,求出整体的int表示        for (int i = 0; i < ips.length; ++i) {            x = Integer.parseInt(ips[i]) + x * 256;        }        List<String> ans = new ArrayList<>();        while (range > 0) {            //求出二进制表示下的最低有效位的位数能表示的地址的数量            //如果为奇数,则=1,即以原单个起始ip地址为第一块            //如果为偶数,则二进制表示下的最低有效位的位数能表示的地址的数量            long step = x & -x;            //如果大于range,则需要缩小范围            while (step > range) step /= 2;            //不大于需要的range,开始处理            //求出现在能表示的step个地址的地址块            ans.add(longToIP(x, (int)step));            //x加上以求出的地址块            x += step;            //range减去以表示的地址块            range -= step;        }//直到range<0        return ans;    }    static String longToIP(long x, int step) {        int[] ans = new int[4];        //&255操作求出后8位十进制表示        ans[0] = (int) (x & 255);        //右移8位,即求下一个块        x >>= 8;        ans[1] = (int) (x & 255);        x >>= 8;        ans[2] = (int) (x & 255);        x >>= 8;        ans[3] = (int) x;        int len = 33;        //每一位就可以表示2个        while (step > 0) {            len --;            step /= 2;        }        return ans[3] + "." + ans[2] + "." + ans[1] + "." + ans[0] + "/" + len;    }}



阅读全文
'); })();
0 0
原创粉丝点击
热门IT博客
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 我有钱 不违农时 不得有违 法不可违教案 法不可违教学设计 违停贴单网上查不到 2019以上8种违建不能强拆 不违 不送帖 雨后小故事2动态图配字 地下城堡2暗月神低配打法 e31230v2配什么主板 起亚k2手动高配多少钱 freebuds2和高配版的区别区别 2米沙发配多大茶几 哈弗h2高配多少钱 2k显示器配什么显卡 地下城堡2裂缝35低配过法 e1230v2配什么主板 红米2a高配版 地下城堡2镇塔恶魔低配打法 e3 1230 v2配什么主板 起亚k2配钥匙多少钱 不配3 水阡墨 禸欲玩具配宅人3中文字幕 红米3s高配版测评 两男一女双3配图解 比亚迪f3低配 比亚迪f3高配多少钱 e31230v3配什么显卡 e3 1231 v3配什么显卡 吉利远景x3高配多少钱 e31231v3配什么主板 禸欲玩具配宅人中文字幕 感喟不已 陕西为什么不重视汉中 四川为什么不重视达州 四川为什么不重视绵阳 形容不重视的成语 山东为什么不重视枣庄 孩子不重视学习