Ai
1 Star 2 Fork 5

LilithSangreal/LeetCode-Solutions

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
powerful-integers.cpp 718 Bytes
一键复制 编辑 原始数据 按行查看 历史
kamyu 提交于 2019-01-06 16:28 +08:00 . Create powerful-integers.cpp
// Time: O((logn)^2), n is the bound
// Space: O(r), r is the size of the result
class Solution {
public:
vector<int> powerfulIntegers(int x, int y, int bound) {
unordered_set<int> result;
int log_x = (x != 1) ? int(floor(log(bound) / log(x))) + 1 : 1;
int log_y = (y != 1) ? int(floor(log(bound) / log(y))) + 1 : 1;
for (int i = 0, pow_x = 1; i < log_x; ++i, pow_x *= x) {
for (int j = 0, pow_y = 1; j < log_y; ++j, pow_y *= y) {
auto val = pow_x + pow_y;
if (val <= bound) {
result.emplace(val);
}
}
}
return vector<int>(result.cbegin(), result.cend());
}
};
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/LilithSangreal/LeetCode-Solutions.git
git@gitee.com:LilithSangreal/LeetCode-Solutions.git
LilithSangreal
LeetCode-Solutions
LeetCode-Solutions
master

搜索帮助