Palindrome Number
Problem
x, decide whether its decimal
representation reads the same forwards and backwards. The minus sign counts as a
character, so every negative number is not a palindrome.x = 121Outputtruex = -121Outputfalsex = 10Outputfalse-231 ≤ x ≤ 231-1 · Follow-up: solve it without converting the integer to a string.
Try it yourself
class Solution {
public boolean isPalindrome(int x) {
}
}
x until the reversed part is ≥ what remains, then compare the two halves.
No string, and no risk of overflow.Instinct Digits, factors, slopes or distances hide a formula - find the invariant, then guard against overflow, negatives and floating-point error.
Memory hack Mod peels, gcd reduces, squares compare - and long saves the day. Peel digits with %10, reduce slopes with gcd, compare squared distances.
Open the Math & Geometry guide →Approaches
Brute ForceConvert to String + Two Pointers
O(log x)Space O(log x)Turn x into its string form and walk two pointers inward,
comparing characters. A negative number fails immediately because
'-' never matches a digit. The string has
~log10x characters.
public boolean isPalindrome(int x) { String s = Integer.toString(x); int left = 0, right = s.length() - 1; while (left < right) { if (s.charAt(left) != s.charAt(right)) return false; left++; right--; } return true; }
Simplest to write, but allocates a string and ignores the follow-up.
BetterReverse All Digits (in a long)
O(log x)Space O(1)Rebuild the number backwards with rev = rev * 10 + x % 10 and
compare it to the original. The reverse of a large int (e.g.
2147483647) can exceed the int range, so accumulate in
a long.
public boolean isPalindrome(int x) { if (x < 0) return false; long reversed = 0; int original = x; while (x != 0) { reversed = reversed * 10 + x % 10; x /= 10; } return reversed == original; }
No string, but processes every digit and needs a wider type to stay safe.
OptimalReverse Only Half the Digits
O(log x)Space O(1)Move digits from x into rev while
x > rev; at that point half the digits have moved. Even length
→ x == rev; odd length → the middle digit sits at the end of
rev, so check x == rev / 10. Numbers ending in
0 (except 0) are rejected up front, because a palindrome cannot start
with 0. rev never exceeds ~5 digits, so it cannot overflow.
public boolean isPalindrome(int x) { if (x < 0 || (x % 10 == 0 && x != 0)) return false; int rev = 0; while (x > rev) { rev = rev * 10 + x % 10; x /= 10; } return x == rev || x == rev / 10; // rev / 10 drops the middle digit }
Dry runx = 12321
- start12321x=12321, rev=0
- iter 11232·x=1232, rev=1
- iter 2123··x=123, rev=12
- iter 312···x=12, rev=123 → stop
- check12···12 == 123/10 ✔
Half the iterations, pure int arithmetic — answers the follow-up.
x % 10 == 0 guard is essential: without it 10
ends with x=0, rev=1 and 0 == 1/10 wrongly returns true.At a glance
| Level | Approach | Time | Space |
|---|---|---|---|
| Brute Force | Convert to String + Two Pointers | O(log x) | O(log x) |
| Better | Reverse All Digits (in a long) | O(log x) | O(1) |
| Optimal | Reverse Only Half the Digits | O(log x) | O(1) |
- Forgetting negatives —
-121reversed is121-, not a palindrome. - Reversing the whole
intinto anintcan overflow for large inputs. - Skipping the trailing-zero check in the half-reverse version (
10→ true).
- Mention the string version first, then tackle the “no string” follow-up.
- Explain why reversing half avoids overflow:
revhas at most 5 digits. - Walk through one odd-length and one even-length example to justify
rev / 10.
% 10, / 10) → reverse, sum or compare the digits of an integer without strings.