Palindrome Number
题目描述
Determine whether an integer is a palindrome. Do this without extra space.
解题方法
extra space
变换成array, 然后方便的得到左右两边的digit
no extra space
因为不能用额外空间,所以不能转化成array再做
尝试用最naive的方法去做, 用数学方法不断得到最大的digit和最小的digit,比较之后将它们去掉,除数的大小也要调整
Solution
class Solution(object):
def isPalindrome(self, x):
"""
:type x: int
:rtype: bool
"""
if x < 0:
return False
largeD = 1
if x < 10:
return True
while x >= largeD:
largeD *= 10
largeD /= 10
while largeD > 1:
firstDigit = x / largeD
lastDigit = x % 10
if firstDigit != lastDigit:
return False
x = x % largeD
x = x / 10
largeD /= 100
return True