Ai
1 Star 2 Fork 5

LilithSangreal/LeetCode-Solutions

Create your Gitee Account
Explore and code with more than 13.5 million developers,Free private repositories !:)
Sign up
文件
Clone or Download
super-palindromes.cpp 1.15 KB
Copy Edit Raw Blame History
kamyu authored 2018-09-16 20:30 +08:00 . Create super-palindromes.cpp
// Time: O(n^0.25 * logn)
// Space: O(logn)
class Solution {
public:
int superpalindromesInRange(string L, string R) {
const auto K = static_cast<int>(pow(10, (R.length() + 1) * 0.25));
const int64_t l = stol(L), r = stol(R);
int result = 0;
// count odd length
for (int k = 0; k < K; ++k) {
const string s = to_string(k), rev_s(s.rbegin(), s.rend());
int64_t v = stol(s + rev_s.substr(1));
v *= v;
if (v > r) {
break;
}
if (v >= l && is_palindrome(v)) {
++result;
}
}
// count even length
for (int k = 0; k < K; ++k) {
const string s = to_string(k), rev_s(s.rbegin(), s.rend());
int64_t v = stol(s + rev_s);
v *= v;
if (v > r) {
break;
}
if (v >= l && is_palindrome(v)) {
++result;
}
}
return result;
}
private:
bool is_palindrome(int64_t k) {
const string s = to_string(k), rev_s(s.rbegin(), s.rend());
return s == rev_s;
}
};
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/LilithSangreal/LeetCode-Solutions.git
git@gitee.com:LilithSangreal/LeetCode-Solutions.git
LilithSangreal
LeetCode-Solutions
LeetCode-Solutions
master

Search