1 Star 0 Fork 0

yuhang2__2/LeetCode-Solutions

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
find-numbers-with-even-number-of-digits.cpp 1.67 KB
一键复制 编辑 原始数据 按行查看 历史
// Time: O(nlog(logm)), n the length of nums, m is the max value of nums
// Space: O(logm)
class Solution {
public:
Solution() {
static const int M = 1e5;
lookup_.emplace_back(0);
int i = 10;
for (; i < M; i *= 10) {
lookup_.emplace_back(i);
}
lookup_.emplace_back(i);
}
int findNumbers(vector<int>& nums) {
return accumulate(nums.cbegin(), nums.cend(), 0,
[this](const auto& x, const auto& y) {
return x + int(digit_count(y) % 2 == 0);
});
}
private:
int digit_count(int n) {
return distance(lookup_.cbegin(),
upper_bound(lookup_.cbegin(), lookup_.cend(), n));
}
vector<int> lookup_;
};
// Time: O(nlogm), n the length of nums, m is the max value of nums
// Space: O(logm)
class Solution2 {
public:
int findNumbers(vector<int>& nums) {
return accumulate(nums.cbegin(), nums.cend(), 0,
[this](const auto& x, const auto& y) {
return x + int(digit_count(y) % 2 == 0);
});
}
private:
int digit_count(int n) {
int result = 0;
for (; n; n /= 10, ++result);
return result;
}
};
// Time: O(nlogm), n the length of nums, m is the max value of nums
// Space: O(logm)
class Solution3 {
public:
int findNumbers(vector<int>& nums) {
return accumulate(nums.cbegin(), nums.cend(), 0,
[](const auto& x, const auto& y) {
return x + int(to_string(y).length() % 2 == 0);
});
}
};
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/yuhang2__2/LeetCode-Solutions.git
git@gitee.com:yuhang2__2/LeetCode-Solutions.git
yuhang2__2
LeetCode-Solutions
LeetCode-Solutions
master

搜索帮助