leetcode 2217. Find Palindrome With Fixed Length (python)

Offer arrives, dig friends to pick up! I am participating in the 2022 Spring Recruitment Check-In Event, click to view the event details .

describe

Given an integer array queries and a positive integer intLength, return an array answer where answer[i] is either the queries[i]th smallest positive palindrome of length intLength or -1 if no such palindrome exists. A palindrome is a number that reads the same backwards and forwards. Palindromes cannot have leading zeros.

Example 1:

Input: queries = [1,2,3,4,5,90], intLength = 3
Output: [101,111,121,131,141,999]
Explanation:
The first few palindromes of length 3 are:
101, 111, 121, 131, 141, 151, 161, 171, 181, 191, 201, ...
The 90th palindrome of length 3 is 999.
复制代码

Example 2:

Input: queries = [2,4,6], intLength = 4
Output: [1111,1331,1551]
Explanation:
The first six palindromes of length 4 are:
1001, 1111, 1221, 1331, 1441, and 1551.
复制代码

Note:

1 <= queries.length <= 5 * 10^4
1 <= queries[i] <= 10^9
1 <= intLength <= 15
复制代码

Parse

According to the meaning of the question, given an integer array queries and a positive integer intLength, return an array answer, where answer[i] is the minimum positive palindrome number of queries[i] of length intLength, if there is no such palindrome number, -1 is returned. The title also gives the definition of a palindrome, which is a number that reads the same before and after. A palindrome cannot have leading zeros.

The maximum length of intLength in the constraints is 15, which means that it is impossible for us to obtain all the palindrome numbers through violent solution, and then find all the results through queries. In fact, if you know the laws of palindrome numbers, you can find all the answers to this question directly through the laws.

When intLength is an even number, if it is 4, we only need to find the minimum number of the first two numbers 10, the maximum is 99, and then reverse the found numbers and splicing them to the back of the first two numbers is the final result, and if The first two numbers are sorted in ascending order, and the number of palindromes spliced ​​at the end is also in ascending order, so we can directly add 10 to queries[i] to obtain the number of palindromes of the specified size.

When intLength is odd, if it is 3, we will temporarily regard it as the size of length 4. Similarly, the first two numbers will find the number n in the range of 10 to 99, but the second half of this time needs to be spliced But it is n[::-1][1:], because intLength is odd, so we can directly add 10 to queries[i] to get the palindrome of the specified size.

The time complexity is O(N) and the space complexity is O(1).

answer

class Solution(object):
    def kthPalindrome(self, queries, intLength):
        """
        :type queries: List[int]
        :type intLength: int
        :rtype: List[int]
        """
        origin = intLength
        if intLength % 2:
            intLength += 1
        k = pow(10, intLength // 2 - 1)
        result = []
        for q in queries:
            tmp = str(k + q - 1)
            if origin % 2:
                tmp += tmp[::-1][1:]
            else:
                tmp += tmp[::-1]
            if len(tmp) == origin:
                result.append(int(tmp))
            else:
                result.append(-1)
        return result	
复制代码

operation result

162 / 162 test cases passed.
Status: Accepted
Runtime: 1042 ms
Memory Usage: 23.3 MB
复制代码

Original title link

leetcode.com/contest/wee…

Your support is my greatest motivation

Guess you like

Origin juejin.im/post/7080695225773096973