We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 0b0d971 commit 0af4c93Copy full SHA for 0af4c93
0009-palindrome-number/solution.py
@@ -1,3 +1,26 @@
1
+# Approach: Reverse Number and compare
2
+
3
+# Time: O(log x)
4
+# Space: O(1)
5
6
class Solution:
- def isPalindrome(self, x: int) -> bool:
- 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