题目来源:
https://leetcode.com/problems/palindrome-number/
题意分析:
这题是要判断一个int是否一个回文数,要求不能申请额外的空间。
题目思路:
这题也是一个简单的题目,由于不能申请额外的空间,所以不能将int转换成string来处理。根据回文数的定义,我们可以考虑将一个int翻转过来,翻转的时候要注意不能超过32位int的最大值。
代码(python):
class Solution(object):
def isPalindrome(self, x):
"""
:type x: int
:rtype: bool
"""
if x < 0:
return False
b = x
ans = 0
while x > 0:
ans = ans*10 + x%10
if x > 2147483647:
return False
x //= 10
if ans == b:
return True
return False
转载请注明出处:http://www.cnblogs.com/chruny/p/4801704.html