From beb45eb5fe5cfb1f159bd96a902c6633e20a32e0 Mon Sep 17 00:00:00 2001 From: Deven <63876261+devenperez@users.noreply.github.com> Date: Wed, 13 Jul 2022 14:46:44 -0400 Subject: [PATCH] Time: 66 ms (81.71%), Space: 13.4 MB (35.24%) - LeetHub --- 9-palindrome-number/9-palindrome-number.py | 34 ++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 9-palindrome-number/9-palindrome-number.py diff --git a/9-palindrome-number/9-palindrome-number.py b/9-palindrome-number/9-palindrome-number.py new file mode 100644 index 0000000..31c9c7a --- /dev/null +++ b/9-palindrome-number/9-palindrome-number.py @@ -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 + \ No newline at end of file