Problem
Given an integer x
, return true
if x
is a
palindrome
, and false otherwise
1 2 3 4
| Palindrome An integer is a palindrome when it reads the same forward and backward.
For example, 121 is a palindrome while 123 is not
|
.
Example 1:
1 2 3 4
| Input: x = 121 Output: true Explanation: 121 reads as 121 from left to right and from right to left.
|
Example 2:
1 2 3 4
| Input: x = -121 Output: false Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.
|
Example 3:
1 2 3 4
| Input: x = 10 Output: false Explanation: Reads 01 from right to left. Therefore it is not a palindrome.
|
Constraints:
Solve
利用字串
1 2 3 4 5 6 7 8
| class Solution: def isPalindrome(self, x: int) -> bool: str_x = str(x) r_str_x = str_x[::-1] if str_x == r_str_x: return True else: return False
|
簡化
1 2 3 4 5 6 7 8
| class Solution: def isPalindrome(self, x: int) -> bool: str_x = str(x)
if str_x == str_x[::-1]: return True return False
|
數學
1 2 3 4 5 6 7 8 9 10 11 12 13
| class Solution: def isPalindrome(self, x: int) -> bool: if x < 0 or (x != 0 and x % 10 == 0): return False
reversed_num = 0 original = x
while x > reversed_num: reversed_num = reversed_num * 10 + x % 10 x //= 10
return x == reversed_num or x == reversed_num // 10
|