requirements
Given an integer x, return true if x is a palindrome integer; Otherwise, return false.
Palindromes are integers that read in positive (left to right) and backward (right to left) order. For example, 121 is palindrome and 123 is not.
Example 1:
Input: x = 121 Output: trueCopy the code
Example 2:
Input: x = -121 Output: false Description: Read from left to right. The value is -121. Read from right to left, 121-. So it's not a palindrome number.Copy the code
Example 3:
Input: x = 10 Output: false Description: Read from right to left, 01. So it's not a palindrome number.Copy the code
Example 4:
Input: x = -101 Output: falseCopy the code
The core code
class Solution:
def isPalindrome(self, x: int) - >bool:
return str(x) == str(x)[::-1]
Copy the code
Another solution
class Solution:
def isPalindrome(self, x: int) - >bool:
xx = x
if x < 0:
return False
reverse = 0
while x > 0:
x,tmp = divmod(x,10)
reverse = reverse * 10 + tmp
return reverse == xx
Copy the code
The first solution is to use a string, i.e. the reverse of the string, to see if it can match. The second solution: use the method of mod by 10 to reverse the newly generated number, and finally compare the original and reversed results are the same.