我是靠谱客的博主 冷酷小甜瓜,最近开发中收集的这篇文章主要介绍ARTS打卡第十八周Algorithm,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

ARTS打卡第十八周

  • Algorithm

Algorithm

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].

Answer:


public static List<String> ipRangeToCidr(String startIp, String endIp) {
List<String> cidrs = new ArrayList<>();
long start = ipToLong(startIp);
long n = ipToLong(endIp) - start + 1;
if (n <= 0) {
throw new IllegalArgumentException("endIp shouldn't be less than startIp");
}
while (n > 0) {
// find the smallest cidr which starts from startIp and within the given range
long step = Long.lowestOneBit(start);
while (step > n) {
step >>= 1;
}
// found the smallest cidr! step indicates the number of IP addresses in the cidr
final long len = calculatePrefixLengthFromNumberOfIpv4Addresses(step);
cidrs.add(ipFromLong(start) + "/" + len);
// move steps
start += step;
n -= step;
}
return cidrs;
}
private static long calculatePrefixLengthFromNumberOfIpv4Addresses(long numberOfIpv4Addresses) {
int len = 32;
for (; numberOfIpv4Addresses > 1; len--) {
numberOfIpv4Addresses >>= 1;
}
return len;
}

最后

以上就是冷酷小甜瓜为你收集整理的ARTS打卡第十八周Algorithm的全部内容,希望文章能够帮你解决ARTS打卡第十八周Algorithm所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(49)

评论列表共有 0 条评论

立即
投稿
返回
顶部