Determine whether an integer is a palindrome. Do this without extra space.
My code:
class Solution(object):
def isPalindrome(self, x):
"""
:type x: int
:rtype: bool
"""
if x<0:
return False
n=len(str(x))
sums = 0
for i in range(n/2):
sums= sums*10+x%10
x = x/10
return (x==sums and n%2==0) or (x/10==sums and n%2!=0)
Reference
class Solution(object):
def isPalindrome(self, x):
"""
:type x: int
:rtype: bool
"""
if x<0 or x!=0 and x%10==0:
return False
sums = 0
while x >sums :
sums= sums*10+x%10
x = x/10
return x==sums or x==sums/10
Note:
1. It is not permitted to convert the number to string to get the digits