Determine whether an integer is a palindrome. Do this without extra space.
The algorithm is easy, we first obtain the number of digits and then we "traverse the number" obtaining the i first digit and i last digit.
Solution:
class Solution:
# @return a boolean
def isPalindrome(self, x):
if x<0:
return False
if x==0:
return True
numberDigits = int(math.log10(x))+1
for i in range(0,numberDigits//2):
beginDigit = (x//10**(numberDigits-i-1))%10
endDigit = (x//10**i)%10
if beginDigit != endDigit:
return False
return True
No comments :
Post a Comment