代码拉取完成,页面将自动刷新
// 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);
});
}
};
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。