Just a Python version of this brilliant C++ solution.
def isPalindrome(self, x):
if x < 0 or (x > 0 and x % 10 == 0): return False
half = 0
while x > half:
half, x = half * 10 + x % 10, x / 10
return x in (half, half / 10)
Just a Python version of this brilliant C++ solution.
def isPalindrome(self, x):
if x < 0 or (x > 0 and x % 10 == 0): return False
half = 0
while x > half:
half, x = half * 10 + x % 10, x / 10
return x in (half, half / 10)
"You could also try reversing an integer. However, if you have solved the problem "Reverse Integer", you know that the reversed integer might overflow. How would you handle such case?"
So the solution is prone to overflow problem, however this is not an issue in python as it handles large values gracefully.