Brute Force:
"""
tc O(lgN) sc O(lgN)
"""
class Solution:
def isPalindrome(self, x: int) -> bool:
if x < 0 :
return False
elif x == 0:
return True
A = []
while x > 0:
A.append(x%10)
x = x//10
return A[::] == A[::-1]
space optimization
"""
tc O(lgN) sc O(1)
"""
class Solution:
def isPalindrome(self, x: int) -> bool:
if x < 0 or (x != 0 and x%10 == 0 ):
return False
reverted_num = 0
# only reverse half of the digits, so 1. to avoid overflow 2. to
while x > reverted_num:
reverted_num = 10 * reverted_num + x % 10
x = x // 10
# in case given x is odd digits, so use reverted_num//10 to remove middle number
return x == reverted_num or x == reverted_num//10