Determine whether an integer is a palindrome. Do this without extra space.
方法一:
思路: 判断一个整数是否为回文结构与Reverse Integer思路几乎相同,稍微修改即可。
1 class Solution(object): 2 def isPalindrome(self, x): 3 """ 4 :type x: int 5 :rtype: bool 6 """ 7 list = [] 8 a = abs(x) 9 y = 0 10 for i in range(len(str(a))): 11 i = a % 10 12 list.append(i) 13 a = a // 10 14 15 list.reverse() 16 17 for j in range(len(list)): 18 y = y + list[j]* 10 ** j 19 if x < 0: 20 y = 0 - y 21 22 return 0<=x==y<2147483648
方法二:
思路:将x转化为字符串再翻转,再将翻转后的字符串转化为整数,判断两个整数是否相等。
1 class Solution(object): 2 def isPalindrome(self, x): 3 """ 4 :type x: int 5 :rtype: bool 6 """ 7 if x < 0: 8 return False 9 else: 10 r = int(str(x)[::-1]) 11 return x == r