LWC 64: 751. IP to CIDR

来源:互联网 发布:淘宝上为啥不卖电视棒 编辑:程序博客网 时间:2024/06/15 14:54

LWC 64: 751. IP to CIDR

传送门:751. IP to CIDR

Problem:

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 = 10
Output: [“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 00000111
The 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 00001000
Addresses with common prefix of 29 bits are:
11111111 00000000 00000000 00001000
11111111 00000000 00000000 00001001
11111111 00000000 00000000 00001010
11111111 00000000 00000000 00001011
11111111 00000000 00000000 00001100
11111111 00000000 00000000 00001101
11111111 00000000 00000000 00001110
11111111 00000000 00000000 00001111

The 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:

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

思路:
题解很取巧,简单说说思路,给定初始的IP之后,转换成2进制的形式,接着每次都找二进制串中的最低位1,它表示的就是CIDR的长度。比如00011000,最低位为00001000,因为在while循环结构内,00011000一定保证在范围内,所以可以认为从00011000开始的step范围内,都是CIDR的某一种解。具体看代码吧。这样一来,我们只需要把00001000转为IP即可,接着让00011000+00001000,继续求解。具体看代码吧!

Java版本:

    public List<String> ipToCIDR(String ip, int range) {        long x = 0;        String[] ips = ip.split("\\.");        for (int i = 0; i < ips.length; ++i) {            x = Integer.parseInt(ips[i]) + x * 256;        }        List<String> ans = new ArrayList<>();        while (range > 0) {            long step = x & -x;  // 最后一个1所在的位置            while (step > range) step /= 2;            ans.add(longToIP(x, (int)step));            x += step;            range -= step;        }        return ans;    }    String longToIP(long x, int step) {        int[] ans = new int[4];        ans[0] = (int) (x & 255); x >>= 8;        ans[1] = (int) (x & 255); x >>= 8;        ans[2] = (int) (x & 255); x >>= 8;        ans[3] = (int) x;        int len = 33;        while (step > 0) {            len --;            step /= 2;        }        return ans[3] + "." + ans[2] + "." + ans[1] + "." + ans[0] + "/" + len;    }

Python版本:

    def ipToCIDR(self, ip, range):        """        :type ip: str        :type range: int        :rtype: List[str]        """        ips = ip.split(".")        x = 0        for num in ips:            x <<= 8            x += int(num)        ans = []        while (range > 0):            step = x & -x            while (step > range): step /= 2            ans.append(self.longToIP(x, step))            x += step            range -= step        return ans    def longToIP(self, x, step):        ans = [0] * 4        ans[0] = x & 255        x >>= 8        ans[1] = x & 255        x >>= 8        ans[2] = x & 255        x >>= 8        ans[3] = x        len = 33        while (step > 0):            len -= 1            step /= 2        return str(ans[3]) + "." + str(ans[2]) + "." + str(ans[1]) + "." + str(ans[0]) + "/" + str(len) 
阅读全文
'); })();
0 0
原创粉丝点击
热门IT博客
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 速卖通假货纠纷怎么办 天猫新开店被恶意拍下怎么办 新开的天猫店没有生意怎么办 银行的支票丢了怎么办 天猫积分用光了怎么办 淘宝店没有无线流量怎么办 京东卖家不发货怎么办会自动打款 一件代发顾客如果退款怎么办 京东以前的账号怎么办 旺旺对话框订单页面变宽了怎么办 阿里巴巴店铺上传图片很模糊怎么办 天猫跨店优惠券用了退货怎么办 淘宝店铺没流量没访客怎么办 京东微信和Q端黑号了怎么办? 买不了运费险了怎么办 拼多多5天不发货怎么办 天猫店手机发货成定制机怎么办 天猫退货上门取件退两件怎么办 买二手苹果手机没有账号怎么办 手机淘宝足迹不更新怎么办 淘宝申请退款不想退了怎么办 荣耀3c主板坏了怎么办 荣耀10天气删了怎么办 荣耀7i手机卡顿怎么办 荣耀v9总是自己拨号怎么办 华为荣耀5x很卡怎么办 华为荣耀6plus卡怎么办 华为手机触屏不灵敏怎么办 华为荣耀6x太卡怎么办 华为荣耀7太卡怎么办 荣耀手机开不开机怎么办 华为g750手机开不了机怎么办 华为手机开不了机怎么办 小米手机刷机后激活不了帐号怎么办 魅蓝note5卡顿怎么办 魅蓝note5卡了怎么办 魅蓝note5很卡怎么办 魅蓝note5锁了怎么办 荣耀9开不开机怎么办 4s更新后用不了怎么办 魅蓝3开不了机怎么办