This problem looks like a string problem at first, but there is a neat numeric solution. The clean trick is to reverse only half of the digits instead of reversing the whole number.
Problem 1: Palindrome Number
Problem description:
Given an integer x, return true if it reads the same forward and backward; otherwise return false. Solve it without converting the number to a string.
What we are solving actually: Reversing the full number works, but it can overflow and does extra work. We only need enough reversed digits to compare the left half with the right half.
What we are doing actually:
- Reject numbers that can never be palindromes, like negatives and numbers ending in
0(except0itself). - Build
reversedHalfone digit at a time from the right side. - Stop once
reversedHalfis greater than or equal to the remaining left half. - Compare the two halves, ignoring the middle digit for odd-length numbers.
class Solution {
public boolean isPalindrome(int x) {
if (x < 0 || (x % 10 == 0 && x != 0)) return false; // Negative numbers and trailing-zero numbers cannot mirror correctly.
int reversedHalf = 0;
while (x > reversedHalf) {
reversedHalf = reversedHalf * 10 + x % 10; // Pull one digit from the right into the reversed half.
x /= 10; // Remove that digit from the original left half.
}
return x == reversedHalf || x == reversedHalf / 10; // Odd-length numbers have one extra middle digit in reversedHalf.
}
}
Debug steps:
- print
xandreversedHalfeach loop iteration - test
121,1221,10,0, and-121 - verify the invariant that
reversedHalfis always the reverse of the digits removed from the original number so far
Why Half Reversal Works
For a palindrome:
- left half mirrors right half
So once we reverse the right half, we do not need the entire reversed number. We only need enough digits to compare both halves.
That gives two cases:
-
even number of digits: compare
x == reversedHalf -
odd number of digits: compare
x == reversedHalf / 10
The middle digit does not matter in a palindrome, so we can drop it.
Dry Run
Input: 1221
- start:
x = 1221,reversedHalf = 0 - move one digit:
reversedHalf = 1x = 122 - move one digit:
reversedHalf = 12x = 12 - stop because
xis no longer greater thanreversedHalf
Now:
x == reversedHalf12 == 12
Answer: true
Odd-length example: 121
At the end:
x = 1reversedHalf = 12
Drop the middle digit:
reversedHalf / 10 = 1
So it is also a palindrome.
Early Rejections
These quick checks are important:
x < 0-> false, because the minus sign appears only on one sidex % 10 == 0 && x != 0-> false, because a non-zero palindrome cannot start with0
Example:
10ends in0- reversed form would need to start with
0 - that is impossible for a normal integer representation
String-Based Alternative
A string solution is completely valid when the problem allows it:
- convert number to string
- compare from both ends
The numeric half-reversal method is useful when:
- the interviewer explicitly forbids string conversion
- you want constant extra space
- you want to avoid whole-number reversal overflow concerns
Common Mistakes
- reversing the full number and risking overflow
- forgetting the trailing-zero rejection rule
- missing the odd-digit comparison with
reversedHalf / 10 - treating negative numbers as possible palindromes
Complexity
- Time:
O(log10 n) - Space:
O(1)
Key Takeaways
- reverse only half the digits, not the whole number
- odd-length palindromes need the middle digit ignored
- early rejection rules remove impossible cases before the loop starts
Comments