forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
find-palindrome-with-fixed-length.py
44 lines (38 loc) · 1.06 KB
/
find-palindrome-with-fixed-length.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# Time: O(n * l)
# Space: O(1)
# math
class Solution(object):
def kthPalindrome(self, queries, intLength):
"""
:type queries: List[int]
:type intLength: int
:rtype: List[int]
"""
def reverse(x):
result = 0
while x:
result = result*10+x%10
x //= 10
return result
def f(l, x):
x = 10**((l-1)//2)+(x-1)
if x > 10**((l+1)//2)-1:
return -1
return x*10**(l//2)+reverse(x//10 if l%2 else x)
return [f(intLength, x) for x in queries]
# Time: O(n * l)
# Space: O(l)
# math
class Solution2(object):
def kthPalindrome(self, queries, intLength):
"""
:type queries: List[int]
:type intLength: int
:rtype: List[int]
"""
def f(l, x):
if 10**((l-1)//2)+(x-1) > 10**((l+1)//2)-1:
return -1
s = str(10**((l-1)//2)+(x-1))
return int(s+s[::-1][l%2:])
return [f(intLength, x) for x in queries]