Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward.
确定整数是否是回文。当一个整数向后读取与向前读取相同的内容时,它就是一个回文。
Example 1:
Input: 121
Output: true
Example 2:
Input: -121
Output: false
Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.
Example 3:
Input: 10
Output: false
Explanation: Reads 01 from right to left. Therefore it is not a palindrome.
Follow up:
Coud you solve it without converting the integer to a string?
判断一个整数(integer)是否是回文,不要使用额外的空间。
生成一个反转整数,通过比较反转整数和原整数是否相等来判断回文。
class Solution:
def isPalindrome(self, x):
if x < 0:
return False
ret = x
y = 0
while ret > 0:
y = y*10 + ret %10
ret = ret//10
if y==x:
return True
else:
return False
if __name__=='__main__':
x=121
a=Solution().isPalindrome(x)
print(a)