2217. Find Palindrome With Fixed Length

Updated: 2024-03-12
1 min read
[]

On This Page

LeetCode problem 2217

class Solution:
    def kthPalindrome(self, queries: List[int], intLength: int) -> List[int]:
        l = (intLength + 1) >> 1
        start, end = 10 ** (l - 1), 10**l - 1
        res = []
        for q in queries:
            v = start + q - 1
            if v > end:
                res.append(-1)
                continue
            s = str(v)
            s += s[::-1][intLength % 2 :]
            res.append(int(s))
        return res