Palindrome Number
Easy
Topics
Given an integer n, return true if it is a palindrome (reads the same backward as forward). Negative numbers are not palindromes.
Example 1
Every digit matches its mirror, so the answer is
true. Reversing only half the number and comparing avoids the overflow that reversing the whole of it risks.Cells are the decimal digits, with their positions below.
Input: n = 121 Output: true
Example 2
The shaded digits differ from their mirror position, so the answer is
false. A minus sign can never mirror, which is why every negative number fails immediately.Input: n = -121 Output: false
Example 3
Input: n = 10 Output: false
Constraints
- -2^31 <= n <= 2^31 - 1
Editorial — intuition, approach, complexity and pitfalls
Intuition
A number is a palindrome exactly when it equals its own digit-reversal. Negatives never qualify because of the leading minus sign.
Approach
- If negative, return False.
- Reverse the digits (or compare the string form with its reverse).
- Return whether they match.
Walkthrough
n = 121 -> "121" reversed "121" -> true
n = -121 -> negative -> false
n = 10 -> "10" vs "01" -> falseComplexity
- Time — O(d): proportional to the digit count d.
- Space — O(1): reversing half the number arithmetically avoids the string entirely for a strict O(1) solution.
Common Pitfalls
- Reverse only the back half. The loop ends when
revcatchesn, which is why nothing can overflow -- a full reversal of a 32-bit palindrome can, and that is the trap the problem is set around. - Odd digit counts need
n == rev // 10as well. Comparing onlyn == revhandles even lengths and reports 121 as not a palindrome. - A note on the negative guard: in this Python form it is documentation rather than correctness.
while n > revis already false for any negative, so -121 returns false with or without it -- verified. Keep it anyway, because the other language tabs do not all share Python's modulo behaviour, and the intent should be visible.
Full Solution — all languages
class Solution:
def isPalindrome(self, n):
# Negatives read as -121 vs 121-, never palindromes.
if n < 0:
return False
# A trailing zero would need a leading zero to match, which no number has.
# (0 itself is the exception.)
if n % 10 == 0 and n != 0:
return False
# Reverse only the back half. The loop stops when rev catches up to n, which
# happens at the midpoint -- so no full reversal, and nothing can overflow
# because rev never grows past n.
rev = 0
while n > rev:
rev = rev * 10 + n % 10
n //= 10
# Even digit count: the halves match exactly.
# Odd: rev carries the middle digit, so drop it before comparing.
return n == rev or n == rev // 10import java.util.*;
class Solution {
public boolean isPalindrome(int n) {
// Negatives read as -121 vs 121-, never palindromes.
if (n < 0) return false;
// A trailing zero would need a leading zero to match. 0 itself is the exception.
if (n % 10 == 0 && n != 0) return false;
// Reverse only the back half. Stopping when rev catches n lands on the midpoint,
// so rev never exceeds n and an int cannot overflow -- which a full reversal of
// 2147483647 would.
int rev = 0;
while (n > rev) {
rev = rev * 10 + n % 10;
n /= 10;
}
// Even digit count: halves match. Odd: rev holds the middle digit, so drop it.
return n == rev || n == rev / 10;
}
}#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
bool isPalindrome(int n) {
// Negatives read as -121 vs 121-, never palindromes.
if (n < 0) return false;
// A trailing zero would need a leading zero to match. 0 itself is the exception.
if (n % 10 == 0 && n != 0) return false;
// Reverse only the back half. Stopping when rev catches n lands on the midpoint,
// so rev never exceeds n and an int cannot overflow.
int rev = 0;
while (n > rev) {
rev = rev * 10 + n % 10;
n /= 10;
}
// Even digit count: halves match. Odd: rev holds the middle digit, so drop it.
return n == rev || n == rev / 10;
}
};/**
* @param {number} n
* @return {boolean}
*/
var isPalindrome = function (n) {
// Negatives read as -121 vs 121-, never palindromes.
if (n < 0) return false;
// A trailing zero would need a leading zero to match. 0 itself is the exception.
if (n % 10 === 0 && n !== 0) return false;
// Reverse only the back half; the loop stops at the midpoint.
let rev = 0;
while (n > rev) {
rev = rev * 10 + (n % 10);
n = Math.floor(n / 10);
}
// Even digit count: halves match. Odd: rev holds the middle digit, so drop it.
return n === rev || n === Math.floor(rev / 10);
};