Ai
1 Star 2 Fork 5

LilithSangreal/LeetCode-Solutions

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
ip-to-cidr.cpp 1.53 KB
一键复制 编辑 原始数据 按行查看 历史
kamyu 提交于 2017-12-25 00:01 +08:00 . Create ip-to-cidr.cpp
// Time: O(n)
// Space: O(1)
class Solution {
public:
vector<string> ipToCIDR(string ip, int range) {
auto start = ipToInt(ip);
vector<string> result;
while (range > 0) {
auto mask = max(33 - bitLength(start & ~(start - 1)),
33 - bitLength(range));
result.emplace_back(intToIP(start) + "/" + to_string(mask));
start += 1 << (32 - mask);
range -= 1 << (32 - mask);
}
return result;
}
private:
uint32_t ipToInt(const string& ip) {
uint32_t result = 0;
for (const auto& n : split(ip, '.')) {
result = 256 * result + stoi(n);
}
return result;
}
string intToIP(uint32_t n) {
string ip;
for (const auto& i : {24, 16, 8, 0}) {
ip += to_string((n >> i) % 256);
ip += ".";
}
ip.pop_back();
return ip;
}
vector<string> split(const string& s, const char delim) {
vector<string> tokens;
stringstream ss(s);
string token;
while (getline(ss, token, delim)) {
tokens.emplace_back(token);
}
return tokens;
}
uint32_t bitLength(uint32_t n) {
uint32_t left = 1, right = 32;
while (left <= right) {
auto mid = left + (right - left) / 2;
if ((1 << mid) > n) {
right = mid - 1;
} else {
left = mid + 1;
}
}
return left;
}
};
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/LilithSangreal/LeetCode-Solutions.git
git@gitee.com:LilithSangreal/LeetCode-Solutions.git
LilithSangreal
LeetCode-Solutions
LeetCode-Solutions
master

搜索帮助