-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathisPalindrome.py
More file actions
26 lines (20 loc) · 770 Bytes
/
isPalindrome.py
File metadata and controls
26 lines (20 loc) · 770 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
"""
Given an integer x, return true if x is palindrome integer.
An integer is a palindrome when it reads the same backward as forward.
For example, 121 is palindrome while 123 is not.
Solution: Converted the number to a string and reversed it
A palindrome number when reversed should be equal to the original
Ps. I found a simplified one liner for the code I have...
Just basically maintain the reversed and the original as strings. No need to convert back to int :)
Simply return this:
return str(num) == (str(num))[::-1]
"""
class Solution:
def isPalindrome(self, x: int) -> bool:
reverse_x = str(x)[::-1]
if x < 0:
return False
elif x == int(reverse_x):
return True
else:
return False