Pyodide¶
# Type here
Run¶
class Solution:
def isPalindrome(self, x: int) -> bool:
if x < 0 or (x % 10 == 0 and x != 0):
return False
reverted_number = 0
while x > reverted_number:
reverted_number = reverted_number * 10 + x % 10
x //= 10
return x == reverted_number or x == reverted_number // 10
# --- Example Usage Code ---
sol = Solution()
print("--- Palindrome Number Examples ---")
# Test Case 1: Standard Palindrome (Odd number of digits)
x1 = 121
result1 = sol.isPalindrome(x1)
print(f"Input: {x1}")
print(f"Output: {result1} (Expected: True)")
# Test Case 2: Standard Palindrome (Even number of digits)
x2 = 1221
result2 = sol.isPalindrome(x2)
print(f"Input: {x2}")
print(f"Output: {result2} (Expected: True)")
# Test Case 3: Not a Palindrome
x3 = 10
result3 = sol.isPalindrome(x3)
print(f"Input: {x3}")
print(f"Output: {result3} (Expected: False - Fails condition 2)")
# Test Case 4: Not a Palindrome (Mismatch)
x4 = 123
result4 = sol.isPalindrome(x4)
print(f"Input: {x4}")
print(f"Output: {result4} (Expected: False)")
# Test Case 5: Negative Number (Fails condition 1)
x5 = -121
result5 = sol.isPalindrome(x5)
print(f"Input: {x5}")
print(f"Output: {result5} (Expected: False)")
# Test Case 6: Zero (Palindrome)
x6 = 0
result6 = sol.isPalindrome(x6)
print(f"Input: {x6}")
print(f"Output: {result6} (Expected: True)")
Solution¶
class Solution:
def isPalindrome(self, x: int) -> bool:
if x < 0 or (x % 10 == 0 and x != 0):
return False
reverted_number = 0
while x > reverted_number:
reverted_number = reverted_number * 10 + x % 10
x //= 10
return x == reverted_number or x == reverted_number // 10
Function Description¶
Source code in python/_9.py
1 2 3 4 5 6 7 8 9 10 11 12 | |
isPalindrome(x)
¶
Source code in python/_9.py
2 3 4 5 6 7 8 9 10 11 12 | |
Explanation¶
9. Palindrome Number (Easy)¶
Given an integer \(x\), the task is to determine whether it is a palindrome. An integer is considered a palindrome if it reads the same forwards and backwards. This definition means that negative numbers, such as \(-121\), cannot be palindromes because the negative sign only appears at the front, so we can immediately return false for any \(x < 0\). Additionally, any number ending in \(0\) (except for \(0\) itself) cannot be a palindrome, as the reversed number would start with \(0\) (e.g., \(120\) reversed is \(021\)).
The most straightforward method to check for a palindrome is to convert the integer into a string. Once in string format, the check is simple: compare the string with its reversed version. However, the problem often includes a "Follow up" to solve it without converting the integer to a string. This requires a numerical approach, typically by either reversing the entire number, as done in problem 7, or by reversing only half of the number to avoid potential overflow issues that can occur when reversing large integers entirely.
The most optimized numerical solution is to reverse only the second half of the integer. We continuously pop the last digit of \(x\) and push it onto a new number, reversed_half, until reversed_half is greater than or equal to \(x\). At this point, we have reversed approximately half of the digits. If the original number \(x\) had an even number of digits, \(x\) must equal reversed_half. If \(x\) had an odd number of digits, the middle digit is naturally ignored, and we check if \(x\) equals reversed_half / 10.