for (int i = 0; i < ips.length; i++) {
num = num * 256 + Integer.parseInt(ips[i]);
}
class Solution {
public List<String> ipToCIDR(String ip, int n) {
List<String> rst = new ArrayList<>();
String[] ips = ip.split("\\.");
long num = 0;
for (int i = 0; i < ips.length; i++) {
num = num * 256 + Integer.parseInt(ips[i]);
}
while (n > 0) {
long step = num & -num;
while (step > n) step /= 2;
rst.add(toIpString(num, step));
num += step;
n -= step;
}
return rst;
}
private String toIpString(long num, long step) {
int[] blocks = new int[4];
blocks[0] = (int) (num & 255);
num >>= 8;
blocks[1] = (int) (num & 255);
num >>= 8;
blocks[2] = (int) (num & 255);
num >>= 8;
blocks[3] = (int) (num & 255);
num >>= 8;
int count = 0;
while (step > 0) {
count++;
step /= 2;
}
return blocks[3] + "." + blocks[2] + "." + blocks[1] + "." + blocks[0] + "/" + (33 - count);
}
}