题目链接:https://leetcode-cn.com/problems/super-palindromes/
题意
给出
L
,
R
L,R
L,R,求在
[
L
,
R
]
[L,R]
[L,R]中是某个回文数的平方的回文数的个数
L
,
R
≤
1
0
18
L,R\leq 10^{18}
L,R≤1018
题解
考虑枚举根号以后的数,这个枚举是1e9的,但因为这个数也是回文数,所以可以枚举前面一半,后面一半复制即可,这样枚举就是1e5的了
code
py代码,py大法好,但跑得很慢竞赛慎用
class Solution:
def superpalindromesInRange(self, left: str, right: str) -> int:
lim = 100000
left, right = int(left), int(right)
def check(x: int) -> int:
return 1 if x >= left and x <= right and str(x)[::-1] == str(x) else 0
cnt = 0
for i in range(1, lim):
s = str(i)
a = int(s+s[::-1])
b = int(s[:-1]+s[::-1])
cnt += check(a*a)+check(b*b)
return cnt