Code
法一:判断一半
时间复杂度:O(log|x|)
空间复杂度:O(1)
class Solution:
def isPalindrome(self, x: int) -> bool:
if(x<0 or (x % 10 == 0 and x != 0)):
return False
rev = 0
while(x > rev):
rev = rev * 10 + x % 10
x = x // 10
return (x == rev) or (x == rev // 10)
法二:字符串反转
class Solution:
def isPalindrome(self, x: int) -> bool:
if(x<0):
return False
if(str(x)[::-1] == str(x)):
return True
else:
return False