Time: 66 ms (81.71%), Space: 13.4 MB (35.24%) - LeetHub

This commit is contained in:
Deven
2022-07-13 14:46:44 -04:00
parent f1d492acfc
commit beb45eb5fe
@@ -0,0 +1,34 @@
class Solution(object):
def isPalindrome(self, x):
"""
:type x: int
:rtype: bool
"""
#print(x)
# All negative numbers are not palindromes
if x < 0:
return False
# Avoids math log error
if x == 0:
return True
currPower = int(math.log(x, 10))
while currPower >= 1:
firstDigit = x / (10 ** currPower)
lastDigit = x % 10
#print([firstDigit, lastDigit])
if firstDigit != lastDigit:
return False
# Remove first and last digit
x -= firstDigit * (10 ** currPower)
x /= 10
currPower -= 2
#print("after " + str(x))
# True if x is one digit (or widdled down to it)
return True