Skip to content

Commit a7cdda5

Browse files
committed
906
1 parent 46f94b5 commit a7cdda5

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

Palindrome/906.md

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
## Super Palindromes
2+
3+
#### Description
4+
5+
[link](https://leetcode.com/problems/super-palindromes/)
6+
7+
---
8+
9+
#### Solution
10+
11+
- See Code
12+
13+
---
14+
15+
#### Code
16+
17+
> O(10^5)
18+
19+
```python
20+
class Solution:
21+
def superpalindromesInRange(self, L: str, R: str) -> int:
22+
L, R = int(L), int(R)
23+
# 确定所有能构建回文的数字的范围,然后判断即可
24+
left = int(math.floor(math.sqrt(L)))
25+
right = int(math.ceil(math.sqrt(R)))
26+
27+
n1, n2 = len(str(left)), len(str(right))
28+
29+
n1 = n1//2 if n1%2==0 else n1//2+1
30+
n2 = n2//2 if n2%2==0 else n2//2+1
31+
32+
start = int('1' + '0'*(n1-1))
33+
end = int('9'*n2)+1
34+
35+
ans = 0
36+
for i in range(start, end):
37+
x = str(i)
38+
num1 = int(x + x[::-1])
39+
num2 = int(x + x[:-1][::-1])
40+
for num in [num1, num2]:
41+
cand = num*num
42+
if cand >= L and cand <= R and str(cand) == str(cand)[::-1]:
43+
ans += 1
44+
return ans
45+
```

0 commit comments

Comments
 (0)