LeetCode: Palindrome Number

Input: x = 121
Output: true
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.
class Solution:
def isPalindrome(self, x: int) -> bool:
if x > 0:
a = int(str(x)[::-1])
if x <= 0:
a = int(str(x*-1)[::-1])
if a == x:
return True
else:
return False

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store