Palindrome Number
Question
Determine whether an integer is a palindrome. Do this without extra space.
Tags
- Mathematics
Thought
Since we cannot use extra space, thus, stack, string, list are all not proper here. Instead, we can utilize the characteristic of the palindrome number here:
The order of the digits from left to right and from right to left should be the same. Thus, we can generate a new integer which is obtained according to the order of the digits in reversed order of the input integer.
Code
class Solution(object):
def isPalindrome(self, x):
"""
:type x: int
:rtype: bool
"""
if x < 0:
return False
if x == 0:
return True
y = 0
tmp = x
while tmp > 0:
y = y * 10 + tmp % 10
tmp /= 10
return x == y