forked from lzl124631x/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths1.cpp
More file actions
38 lines (38 loc) · 1.14 KB
/
s1.cpp
File metadata and controls
38 lines (38 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// OJ: https://leetcode.com/problems/super-palindromes/
// Author: github.com/lzl124631x
// Time: O(W^(1/4)*logW)
// Space: O(logW)
// Ref: https://leetcode.com/problems/super-palindromes/solution/
typedef unsigned long long ULL;
class Solution {
private:
bool isPalindrome(ULL n) {
ULL r = 0, num = n;
while (n) {
r = r * 10 + n % 10;
n /= 10;
}
return r == num;
}
public:
int superpalindromesInRange(string L, string R) {
ULL i = 1, ans = 0, nl = stoull(L), nr = stoull(R), MAGIC = 100000;
for (int k = 1; k < MAGIC; ++k) {
string s = to_string(k);
for (int i = s.size() - 2; i >= 0; --i) s += s[i];
ULL n = stoull(s);
n *= n;
if (n > nr) break;
if (n >= nl && isPalindrome(n)) ++ans;
}
for (int k = 1; k < MAGIC; ++k) {
string s = to_string(k);
for (int i = s.size() - 1; i >= 0; --i) s += s[i];
ULL n = stoull(s);
n *= n;
if (n > nr) break;
if (n >= nl && isPalindrome(n)) ++ans;
}
return ans;
}
};