The goal is to determine if an integer reads the same backward as forward. Pattern: Integer reversal / Half-reversal.
A common first instinct is to convert the integer to a string and check if the string equals its reverse. However, solving it without converting to a string is an excellent exercise in fundamental math operations (modulo and division).
The optimal approach is to reverse only the second half of the number and compare it to the first half. This prevents potential integer overflow.
How do we know we’ve reached the halfway point? When the reversed number is greater than or equal to the original number (which is being reduced at each step).
Pseudocode:
- Convert the integer
xto a string. - Check if the string is equal to its reversed version.
- Handle negative numbers (which can never be palindromes due to the
-sign).
class Solution {
public:
bool isPalindrome(int x) {
if (x < 0) {
return false;
}
string s = to_string(x);
string rev_s = s;
reverse(rev_s.begin(), rev_s.end());
return s == rev_s;
}
};Pseudocode:
- Return false if
x < 0or ifxends in 0 (but is not 0 itself), as leading zeros are invalid. - Initialize
revertedNumber = 0. - Loop while
x > revertedNumber:revertedNumber = revertedNumber * 10 + x % 10x /= 10
- When the loop ends, check if
x == revertedNumber(even length) orx == revertedNumber / 10(odd length).
class Solution {
public:
bool isPalindrome(int x) {
// Special cases:
// 1. Negative numbers are not palindromes.
// 2. If the last digit is 0, the first digit must also be 0.
// Only 0 satisfies this property.
if (x < 0 || (x % 10 == 0 && x != 0)) {
return false;
}
int revertedNumber = 0;
while (x > revertedNumber) {
revertedNumber = revertedNumber * 10 + x % 10;
x /= 10;
}
// When the length is an odd number, we can get rid of the middle digit by revertedNumber / 10
// For example, with 12321, at the end of the loop x = 12, revertedNumber = 123.
return x == revertedNumber || x == revertedNumber / 10;
}
};- Brute Force (String):
- Time: where is the number of digits. Converting integer to string takes time proportional to the number of digits.
- Space: to store the string representation.
- Optimal (Half-Reversal):
- Time: . We divide the input by 10 for every iteration, meaning we run the loop for half the number of digits in .
- Space: . We only use a single integer variable
revertedNumber.
- Negative Numbers:
-121reversed is121-, which is not equal to-121. Handled cleanly with an initialif (x < 0)check. - Trailing Zeros:
10reversed is01, which mathematically equals1, not10.x % 10 == 0 && x != 0immediately catches these without extra math. - Single Digits: A number like
5will immediately skip thewhile (x > revertedNumber)loop and returnTrue, sincex == revertedNumber. - Zero:
0returnsTrue, bypassing the modulo zero check because ofx != 0.
Thought Process & Recognition:
Problems that ask to manipulate an integer mathematically without strings often rely heavily on % 10 to pop the last digit and / 10 to shift the number down.
When trying to prevent integer overflow in reversal problems, the key insight is to stop halfway. For palindromes, halfway is all you need to compare both sides. If you see “palindrome” and “integer”, immediately think “pop and push digits mathematically” until the new number matches or exceeds the remaining original number.