美文网首页
*751. IP to CIDR

*751. IP to CIDR

作者: bin_guo | 来源:发表于2018-07-02 10:48 被阅读0次

Leetcode: 751. IP 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:
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:
  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].
Hints:

x&-x 操作的含义: 取出x中最低为1的那一位,例如:

  • 2:二进制为0000 0010,最低为1的那一位是第2位,所以取出后为0000 0010
  • 3:二进制为0000 0011,最低为1的那一位是第1位,所以取出后为0000 0001
  • 6:二进制为0000 0110,最低为1的那一位是第2位,所以取出后为0000 0010
  • 31:二进制为0001 1111,最低为1的那一位是第1位,所以取出后为0000 0001
  • 32:二进制为0010 0000,最低为1的那一位是第6位,所以取出后为0010 0000
Solution:
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;
    }
}

Referenced from
Author: link_98
Link: https://blog.csdn.net/qq_32832023/article/details/78891298

相关文章

  • *751. IP to CIDR

    Leetcode: 751. IP to CIDRGiven a start IP address ip and ...

  • stringstream

    stringstream与getline的配合 今天在做leetcode题目(#### IP 到 CIDR)时,需...

  • PCL-计算机网络

    IP和CIDR 路由器不会对私有网段的包进行转发 超网(CIDR)路由通道。 路由聚合 目的:骨干网路由条目实在太...

  • 运维面试题1-100

    1、NAT和PAT的区别 IP地址耗尽促成了CIDR的开发,但是CIDR开发的主要目的是为了有效的使用现有的INT...

  • 五、IP/VLSM/CIDR

    个人学习笔记,若有侵权,请告知! 目录 IP IP地址 网络层概念 网络设备的身份标识,保证主机间正常通信 一种网...

  • CIDR

    CIDR(Classless Inter-Domain Routing)子网划分 1. 原始的IP地址表示方法及其...

  • IP地址掩码以及CIDR掩码理解计算问题

    网络层复习:IP地址及子网掩码及CIDR掩码 IP地址:因为早期设置问题ipv4占32位,ip地址由网络号+主机号...

  • 02 IP地址、 MAC 地址、 CIDR

    目录: https://www.jianshu.com/p/1961df2a1336 IP地址格式与分类: IP ...

  • TCP/IP之CIDR与路由聚合

    CIDR (CIDR: Classless InterDomain Routing)无类域间路由 消除传统的 A ...

  • golang CIDR 与 IpMask 互转解析

    在网络设备上,每家厂商表示ip的形式各不相同,但可能表示的是同一个意思,列如 CIDR表示方法:IP地址/网络ID...

网友评论

      本文标题:*751. IP to CIDR

      本文链接:https://www.haomeiwen.com/subject/gmvkuftx.html