LeetCode Problem Workspace

Palindrome Number

Determine if a given integer reads the same forward and backward using a math-driven solution strategy without converting to string.

category

1

Topics

code_blocks

10

Code langs

hub

3

Related

Practice Focus

Easy · Math-driven solution strategy

bolt

Answer-first summary

Determine if a given integer reads the same forward and backward using a math-driven solution strategy without converting to string.

Interview AiBox logo

Ace coding interviews with Interview AiBox guidance for Math-driven solution strategy

Try AiBox Copilotarrow_forward

This problem requires returning true if the integer reads identically forward and backward, false otherwise. A math-driven solution avoids string conversion and focuses on reversing half of the number to compare digits. The approach must handle negative numbers and avoid integer overflow during reversal for correctness and efficiency.

Problem Statement

Given an integer x, determine whether it reads the same forward and backward. Negative numbers are never palindromes, and single-digit numbers are always palindromes. You must implement a solution using numeric operations rather than converting the integer to a string.

Implement a function that takes an integer x and returns true if x is a palindrome. Examples include x = 121 returning true, x = -121 returning false, and x = 10 returning false. The solution must work within the integer range -2^31 <= x <= 2^31 - 1.

Examples

Example 1

Input: x = 121

Output: true

121 reads as 121 from left to right and from right to left.

Example 2

Input: x = -121

Output: false

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

Reads 01 from right to left. Therefore it is not a palindrome.

Constraints

  • -231 <= x <= 231 - 1

Solution Approach

Reverse Half the Number

Instead of reversing the entire integer, reverse only half of it and compare with the remaining half. This avoids overflow and improves efficiency. Stop reversing when the reversed half is greater than or equal to the remaining half.

Handle Negative and Trailing Zero Cases

Immediately return false for negative numbers and numbers ending with 0 (except 0 itself). These are guaranteed non-palindromes. This prevents unnecessary computation and edge case errors.

Compare Digits Digit-by-Digit

Extract digits using modulo and integer division to reverse the number mathematically. Compare each digit with the corresponding one from the other half. This ensures correctness without string manipulation and follows the math-driven pattern.

Complexity Analysis

Metric Value
Time Depends on the final approach
Space Depends on the final approach

Time complexity is O(log10(n)) because each step processes one digit, reducing the number by a factor of 10. Space complexity is O(1) since no additional data structures are used beyond simple integer variables.

What Interviewers Usually Probe

  • Checks if you can handle numeric operations without string conversion.
  • Sees if you consider edge cases like negatives and trailing zeros.
  • Wants to test knowledge of integer overflow and efficient reversal.

Common Pitfalls or Variants

Common pitfalls

  • Reversing the entire number can cause integer overflow.
  • Forgetting to handle negative numbers or numbers ending in 0.
  • Using string conversion instead of math-driven digit operations.

Follow-up variants

  • Check palindrome for a number represented as a linked list.
  • Determine if a number is a palindrome in a different base, e.g., binary.
  • Find the longest palindromic subsequence within the integer's digits.

FAQ

Can negative numbers ever be palindromes?

No, any negative number is automatically not a palindrome because the minus sign does not mirror at the end.

What is the fastest approach for checking a palindrome number?

Reversing only half of the digits and comparing with the other half is fastest, avoiding overflow and extra space.

Why not convert the number to a string?

String conversion is simpler but violates the math-driven pattern and uses extra space, which may be discouraged in interviews.

How do I handle numbers ending with zero?

Numbers ending with zero are not palindromes unless the number itself is zero; this prevents incorrect matches.

Does the pattern always involve reversing digits?

Yes, for the Math-driven solution strategy, reversing digits and comparing halves is the core method to check palindromes.

terminal

Solution

Solution 1: Reverse Half of the Number

First, we determine special cases:

1
2
3
4
5
6
7
8
9
class Solution:
    def isPalindrome(self, x: int) -> bool:
        if x < 0 or (x and x % 10 == 0):
            return False
        y = 0
        while y < x:
            y = y * 10 + x % 10
            x //= 10
        return x in (y, y // 10)
Palindrome Number Solution: Math-driven solution strategy | LeetCode #9 Easy