LeetCode 906 Super Palindromes (Python)

Posted by 小明MaxMing on May 8, 2021

题目

Let’s say a positive integer is a super-palindrome if it is a palindrome, and it is also the square of a palindrome.

Given two positive integers left and right represented as strings, return the number of super-palindromes integers in the inclusive range [left, right].

解题思路

构造回文数,然后判读其平方是否也是回文数,根据题目的数据范围,最大100000能构成题目要求的超级回文数,构造的时候需要构造长度为奇数和偶数

代码

class Solution:
    def superpalindromesInRange(self, left: str, right: str) -> int:
        def check(x):
            return str(x) == str(x)[::-1]
        
        left, right = int(left), int(right)
        upper = 100000
        res = 0
        
        for i in range(upper):
            s = str(i)
            t = int(s + s[-2::-1]) ** 2
            if t > right:
                break
            if t >= left and check(t):
                res += 1
                
        for i in range(upper):
            s = str(i)
            t = int(s + s[::-1]) ** 2
            if t > right:
                break
            if t >= left and check(t):
                res += 1
        return res

视频讲解 YouTube<--欢迎点击订阅

视频讲解 bilibili<--欢迎点击订阅