给你一个整数 x ,如果 x 是一个回文整数,返回 true ;否则,返回 false 。
第一种:将整数转成字符串处理
class Solution:
def isPalindrome(self, x: int) -> bool:
if x < 0:
return False
x = str(x)
if x[::-1] == x:
return True
else:
return False
第二种:数字处理
class Solution:
def isPalindrome(self, x: int) -> bool:
total = 0
temp = x
while x > 0:
total = total * 10 + x % 10
x //= 10
return total == temp