mirror of
https://github.com/devenperez/leetcode.git
synced 2026-06-13 23:07:08 +00:00
Time: 66 ms (81.71%), Space: 13.4 MB (35.24%) - LeetHub
This commit is contained in:
@@ -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
|
||||||
|
|
||||||
Reference in New Issue
Block a user