Created
September 7, 2021 14:16
-
-
Save codecakes/0e16a69b0ece420be237823f0db0628e to your computer and use it in GitHub Desktop.
is it a palindrome integer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import math | |
| class Solution: | |
| def isPalindrome(self, x): | |
| ''' | |
| :type x: int | |
| :rtype: bool | |
| ''' | |
| if x < 0: | |
| return False | |
| if x == 0: | |
| return True | |
| num_ln = math.ceil(math.log10(x)) | |
| ten_pow_den = pow(10, num_ln-1) | |
| return self.helper(ten_pow_den, num_ln, x) | |
| def helper(self, ten_pow_den, num_ln, x): | |
| # print(f"num_ln={num_ln} ten_pow_den={ten_pow_den} x={x}") | |
| if num_ln <= 0: return True | |
| lhs_num, rhs_num = x//ten_pow_den, x%10 | |
| # print(lhs_num, rhs_num) | |
| if lhs_num != rhs_num: | |
| return False | |
| num_ln -= 2 | |
| new_x = (x%ten_pow_den)//10 | |
| ten_pow_den = pow(10, num_ln-1) | |
| return self.helper(ten_pow_den, num_ln, new_x) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment