Skip to content

Commit 0af4c93

Browse files
committed
Sync LeetCode submission Runtime - 12 ms (29.32%), Memory - 17.9 MB (24.36%)
1 parent 0b0d971 commit 0af4c93

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

0009-palindrome-number/solution.py

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,26 @@
1+
# Approach: Reverse Number and compare
2+
3+
# Time: O(log x)
4+
# Space: O(1)
5+
16
class Solution:
2-
def isPalindrome(self, x: int) -> bool:
3-
return str(x) == str(x)[::-1]
7+
def isPalindrome(self, x: int) -> bool:
8+
# Negative numbers are not palindrome
9+
if x < 0:
10+
return False
11+
12+
# Single digit numbers are palindrome
13+
if x < 10:
14+
return True
15+
16+
# Reverse the number and compare
17+
original = x
18+
reversed_num = 0
19+
20+
while x > 0:
21+
digit = x % 10
22+
reversed_num = reversed_num * 10 + digit
23+
x //= 10
24+
25+
return original == reversed_num
26+

0 commit comments

Comments
 (0)