地址:点击打开链接
题目要求判断是否回文数字,而且要求不能用额外空间,意味着将数字转字符串不行,将数字颠倒也不行,那只有判断高位和地位数字是否相同了,而且判断条件要注意
答案:
class Solution(object):
def isPalindrome(self, x):
"""
:type x: int
:rtype: bool
"""
if x <= 9 and x >= 0:
return True
if x < 0:
return False
low = 0
high = 0
base = 10
while x / base >= 10:
base *= 10
while x != 0:
low = x % 10
high = x / base
if low != high:
return False
x %= base
x /= 10
base /= 100
return True