Given an integer x
, return true
if x
is a palindrome, and false
otherwise.
Example 1:
Input: x = 121 Output: true Explanation: 121 reads as 121 from left to right and from right to left.
Example 2:
Input: x = -121 Output: false Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.
Example 3:
Input: x = 10 Output: false Explanation: Reads 01 from right to left. Therefore it is not a palindrome.
Constraints:
-231 <= x <= 231 - 1
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The most straightforward way to see if a number is a palindrome is to first figure out what the number looks like when its digits are written in reverse order. Then, you simply compare this new reversed number to the original one.
Here's how the algorithm would work step-by-step:
class Solution:
def isPalindrome(self, original_number: int) -> bool:
# A negative number cannot be a palindrome because the minus sign breaks the symmetry.
if original_number < 0:
return False
number_to_reverse = original_number
reversed_number = 0
# Reverse the number by repeatedly taking the last digit and adding it to the new number.
while number_to_reverse > 0:
last_digit = number_to_reverse % 10
reversed_number = (reversed_number * 10) + last_digit
number_to_reverse = number_to_reverse // 10
# The number is a palindrome if its reversed form is identical to the original.
return original_number == reversed_number
The core idea is to reverse only half of the number and then compare it to the other half. This avoids dealing with the whole number at once and sidesteps potential issues with very large numbers.
Here's how the algorithm would work step-by-step:
def is_palindrome(input_number):
# Negative numbers cannot be palindromes. Also, if a number ends in 0 (and is not 0 itself),
# its reverse would start with 0, so it can't be a palindrome.
if input_number < 0 or (input_number % 10 == 0 and input_number != 0):
return False
reverted_second_half = 0
remaining_first_half = input_number
# We only need to reverse half the number to avoid potential integer overflow and improve performance.
while remaining_first_half > reverted_second_half:
last_digit = remaining_first_half % 10
reverted_second_half = reverted_second_half * 10 + last_digit
remaining_first_half //= 10
# For numbers with an odd number of digits, the middle digit will be the last digit of the
# reverted half. We can remove it by integer division as it doesn't affect the palindrome check.
return remaining_first_half == reverted_second_half or remaining_first_half == reverted_second_half // 10
Case | How to Handle |
---|---|
Negative numbers (e.g., -121) | Negative numbers are never palindromes because the negative sign breaks the symmetry, so the function should return false immediately. |
Single-digit numbers (0-9) | Single-digit numbers are always palindromes, and the algorithm correctly handles this by reversing the number to itself. |
Numbers ending in zero (e.g., 10, 120) | If a positive number ends in zero, it cannot be a palindrome (except for 0 itself), so we can return false early. |
The number is exactly zero | Zero is a palindrome, and the logic correctly handles this by returning true as it's a single-digit number. |
The number is the maximum integer value (e.g., 2^31 - 1) | Reversing a large number like the max integer value can cause an integer overflow, which must be prevented by using a 64-bit integer for the reversed number or by reversing only half the number. |
A number with an even number of digits (e.g., 1221) | The half-reversal approach works correctly by comparing the first half with the reversed second half when both halves are equal. |
A number with an odd number of digits (e.g., 12321) | The half-reversal approach handles this by comparing the first half with the reversed second half after removing the middle digit. |
The input is not a valid integer (e.g., null, undefined, string) | Depending on language and constraints, the solution might throw a type error or require explicit type checking at the beginning. |