-
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathsolution.py
More file actions
16 lines (14 loc) · 598 Bytes
/
Copy pathsolution.py
File metadata and controls
16 lines (14 loc) · 598 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution:
# Time: O(log10(n)) - process half the digits
# Space: O(1)
def is_palindrome(self, x: int) -> bool:
# Negative numbers and numbers ending in 0 (except 0 itself) are not palindromes
if x < 0 or (x % 10 == 0 and x != 0):
return False
reversed_half = 0
while x > reversed_half:
reversed_half = reversed_half * 10 + x % 10
x //= 10
# Even length: x == reversed_half
# Odd length: x == reversed_half // 10 (drop middle digit)
return x == reversed_half or x == reversed_half // 10