classSolution:defisPalindrome(self, x:int)->bool:
s =str(x)return s == s[::-1]
publicclassSolution{publicbooleanisPalindrome(int x){String s =String.valueOf(x);return s.equals(newStringBuilder(s).reverse().toString());}}
classSolution{public:boolisPalindrome(int x){
string s =to_string(x);
string rev = s;reverse(rev.begin(), rev.end());return s == rev;}};
classSolution{/**
* @param {number} x
* @return {boolean}
*/isPalindrome(x){const s =String(x);return s === s.split('').reverse().join('');}}
publicclassSolution{publicboolIsPalindrome(int x){string s = x.ToString();char[] arr = s.ToCharArray();
Array.Reverse(arr);string rev =newstring(arr);return s == rev;}}
funcisPalindrome(x int)bool{
s := strconv.Itoa(x)
n :=len(s)for i :=0; i < n/2; i++{if s[i]!= s[n-i-1]{returnfalse}}returntrue}
class Solution {funisPalindrome(x: Int): Boolean {val s = x.toString()return s == s.reversed()}}
classSolution{funcisPalindrome(_ x:Int)->Bool{let s =String(x)return s ==String(s.reversed())}}
Time & Space Complexity
Time complexity: O(n)
Space complexity: O(n)
Where n is the number of digits in the given integer.
2. Convert to String (Optimal)
Intuition
Instead of creating a reversed copy of the entire string, we can compare characters from both ends moving toward the center. This avoids the extra space needed for the reversed string.
We only need to check half the string since we compare characters in pairs.
Algorithm
Convert the integer to its string representation.
Initialize two pointers: i at the start and j at the end.
While i < n/2, compare s[i] with s[n - i - 1]. If they differ, return false.
classSolution:defisPalindrome(self, x:int)->bool:
s =str(x)
n =len(s)for i inrange(n //2):if s[i]!= s[n - i -1]:returnFalsereturnTrue
publicclassSolution{publicbooleanisPalindrome(int x){String s =String.valueOf(x);int n = s.length();for(int i =0; i < n /2; i++){if(s.charAt(i)!= s.charAt(n - i -1)){returnfalse;}}returntrue;}}
classSolution{public:boolisPalindrome(int x){
string s =to_string(x);int n = s.length();for(int i =0; i < n /2; i++){if(s[i]!= s[n - i -1]){returnfalse;}}returntrue;}};
classSolution{/**
* @param {number} x
* @return {boolean}
*/isPalindrome(x){const s =String(x);let n = s.length;for(let i =0; i < n >>1; i++){if(s.charAt(i)!= s.charAt(n - i -1)){returnfalse;}}returntrue;}}
publicclassSolution{publicboolIsPalindrome(int x){string s = x.ToString();int n = s.Length;for(int i =0; i < n /2; i++){if(s[i]!= s[n - i -1]){returnfalse;}}returntrue;}}
funcisPalindrome(x int)bool{
s := strconv.Itoa(x)
n :=len(s)for i :=0; i < n/2; i++{if s[i]!= s[n-i-1]{returnfalse}}returntrue}
class Solution {funisPalindrome(x: Int): Boolean {val s = x.toString()val n = s.length
for(i in0 until n /2){if(s[i]!= s[n - i -1]){returnfalse}}returntrue}}
classSolution{funcisPalindrome(_ x:Int)->Bool{let s =Array(String(x))let n = s.count
for i in0..<(n /2){if s[i]!= s[n - i -1]{returnfalse}}returntrue}}
Time & Space Complexity
Time complexity: O(n)
Space complexity: O(n)
Where n is the number of digits in the given integer.
3. Reverse the Integer
Intuition
Without using string conversion, we can reverse the entire integer mathematically and compare it to the original. If they are equal, the number is a palindrome.
Negative numbers cannot be palindromes because of the negative sign. We build the reversed number digit by digit using modulo and division operations.
Algorithm
If the number is negative, return false immediately.
Initialize rev = 0 and num = x.
While num > 0:
Extract the last digit using num % 10.
Append it to rev by computing rev = rev * 10 + digit.
Remove the last digit from num using integer division.
Compare rev with the original x and return true if equal.
classSolution:defisPalindrome(self, x:int)->bool:if x <0:returnFalse
rev =0
num = x
while num:
rev =(rev *10)+(num %10)
num //=10return rev == x
publicclassSolution{publicbooleanisPalindrome(int x){if(x <0){returnfalse;}long rev =0, num = x;while(num !=0){
rev =(rev *10)+(num %10);
num /=10;}return rev == x;}}
classSolution{public:boolisPalindrome(int x){if(x <0){returnfalse;}longlong rev =0, num = x;while(num !=0){
rev =(rev *10)+(num %10);
num /=10;}return rev == x;}};
classSolution{/**
* @param {number} x
* @return {boolean}
*/isPalindrome(x){if(x <0){returnfalse;}let rev =0,
num = x;while(num !==0){
rev = rev *10+(num %10);
num = Math.floor(num /10);}return rev === x;}}
publicclassSolution{publicboolIsPalindrome(int x){if(x <0)returnfalse;int rev =0, num = x;while(num !=0){
rev = rev *10+ num %10;
num /=10;}return rev == x;}}
funcisPalindrome(x int)bool{if x <0{returnfalse}
rev, num :=0, x
for num !=0{
rev = rev*10+ num%10
num /=10}return rev == x
}
class Solution {funisPalindrome(x: Int): Boolean {if(x <0)returnfalsevar rev =0Lvar num = x
while(num !=0){
rev = rev *10+ num %10
num /=10}return rev == x.toLong()}}
classSolution{funcisPalindrome(_ x:Int)->Bool{if x <0{returnfalse}var rev =0var num = x
while num !=0{
rev = rev *10+ num %10
num /=10}return rev == x
}}
Time & Space Complexity
Time complexity: O(n)
Space complexity: O(1)
Where n is the number of digits in the given integer.
4. Two Pointers
Intuition
We can compare digits from both ends without converting to a string or reversing the entire number. The idea is to extract the leftmost and rightmost digits and compare them.
We use a divisor to extract the leftmost digit and modulo to extract the rightmost digit, then shrink the number from both ends.
Algorithm
If the number is negative, return false.
Find the divisor div such that x / div gives the first digit. This is the largest power of 10 less than or equal to x.
While x > 0:
Compare the first digit (x / div) with the last digit (x % 10). If they differ, return false.
Remove both the first and last digits: x = (x % div) / 10.
Update the divisor: div /= 100 (since we removed two digits).
classSolution:defisPalindrome(self, x:int)->bool:if x <0:returnFalse
div =1while x >=10* div:
div *=10while x:if x // div != x %10:returnFalse
x =(x % div)//10
div //=100returnTrue
publicclassSolution{publicbooleanisPalindrome(int x){if(x <0){returnfalse;}long div =1;while(x >=10* div){
div *=10;}while(x !=0){if(x / div != x %10){returnfalse;}
x =(int)(x % div)/10;
div /=100;}returntrue;}}
classSolution{public:boolisPalindrome(int x){if(x <0){returnfalse;}longlong div =1;while(x >=10* div){
div *=10;}while(x !=0){if(x / div != x %10){returnfalse;}
x =(x % div)/10;
div /=100;}returntrue;}};
classSolution{/**
* @param {number} x
* @return {boolean}
*/isPalindrome(x){if(x <0){returnfalse;}let div =1;while(x >=10* div){
div *=10;}while(x !==0){if(Math.floor(x / div)!== x %10){returnfalse;}
x = Math.floor((x % div)/10);
div = Math.floor(div /100);}returntrue;}}
publicclassSolution{publicboolIsPalindrome(int x){if(x <0)returnfalse;int div =1;while(x / div >=10){
div *=10;}while(x !=0){if(x / div != x %10){returnfalse;}
x =(x % div)/10;
div /=100;}returntrue;}}
funcisPalindrome(x int)bool{if x <0{returnfalse}
div :=1for x >=10*div {
div *=10}for x !=0{if x/div != x%10{returnfalse}
x =(x % div)/10
div /=100}returntrue}
class Solution {funisPalindrome(x: Int): Boolean {if(x <0)returnfalsevar num = x
var div =1Lwhile(num >=10* div){
div *=10}while(num !=0){if(num / div !=(num %10).toLong()){returnfalse}
num =((num % div)/10).toInt()
div /=100}returntrue}}
classSolution{funcisPalindrome(_ x:Int)->Bool{if x <0{returnfalse}var num = x
var div =1while num >=10* div {
div *=10}while num !=0{if num / div != num %10{returnfalse}
num =(num % div)/10
div /=100}returntrue}}
Time & Space Complexity
Time complexity: O(n)
Space complexity: O(1)
Where n is the number of digits in the given integer.
5. Reverse Half of the Number
Intuition
Reversing the entire number risks integer overflow. A clever optimization is to reverse only the second half of the number and compare it to the first half.
We keep extracting digits from the end and building the reversed half until the reversed half is greater than or equal to the remaining number. At that point, we have processed half (or just past half) of the digits.
Algorithm
Handle edge cases: negative numbers and numbers ending in 0 (except 0 itself) are not palindromes.
Initialize rev = 0.
While x > rev:
Extract the last digit of x and append it to rev.
Remove the last digit from x.
After the loop, compare x == rev (even length) or x == rev / 10 (odd length, where the middle digit is in rev).
classSolution:defisPalindrome(self, x:int)->bool:if x <0or(x !=0and x %10==0):returnFalse
rev =0while x > rev:
rev =(rev *10)+(x %10)
x //=10return x == rev or x == rev //10
publicclassSolution{publicbooleanisPalindrome(int x){if(x <0||(x !=0&& x %10==0)){returnfalse;}int rev =0;while(x > rev){
rev = rev *10+ x %10;
x /=10;}return x == rev || x == rev /10;}}
classSolution{public:boolisPalindrome(int x){if(x <0||(x !=0&& x %10==0)){returnfalse;}int rev =0;while(x > rev){
rev = rev *10+ x %10;
x /=10;}return x == rev || x == rev /10;}};
classSolution{/**
* @param {number} x
* @return {boolean}
*/isPalindrome(x){if(x <0||(x !==0&& x %10===0)){returnfalse;}let rev =0;while(x > rev){
rev = rev *10+(x %10);
x = Math.floor(x /10);}return x === rev || x === Math.floor(rev /10);}}
publicclassSolution{publicboolIsPalindrome(int x){if(x <0||(x !=0&& x %10==0)){returnfalse;}int rev =0;while(x > rev){
rev = rev *10+ x %10;
x /=10;}return x == rev || x == rev /10;}}
funcisPalindrome(x int)bool{if x <0||(x !=0&& x%10==0){returnfalse}
rev :=0for x > rev {
rev = rev*10+ x%10
x /=10}return x == rev || x == rev/10}
class Solution {funisPalindrome(x: Int): Boolean {if(x <0||(x !=0&& x %10==0)){returnfalse}var num = x
var rev =0while(num > rev){
rev = rev *10+ num %10
num /=10}return num == rev || num == rev /10}}
classSolution{funcisPalindrome(_ x:Int)->Bool{if x <0||(x !=0&& x %10==0){returnfalse}var num = x
var rev =0while num > rev {
rev = rev *10+ num %10
num /=10}return num == rev || num == rev /10}}
Time & Space Complexity
Time complexity: O(n)
Space complexity: O(1)
Where n is the number of digits in the given integer.
Common Pitfalls
Forgetting Negative Numbers Are Not Palindromes
Negative numbers have a minus sign at the front, so they can never be palindromes (e.g., -121 reads as 121- backwards). Failing to handle negative numbers as an early return case leads to incorrect results or issues with modulo operations on negative values.
Not Handling Numbers Ending in Zero
Numbers ending in zero (except zero itself) cannot be palindromes because leading zeros are not allowed in integers. For example, 10 would need to be 01 reversed, which is not a valid representation. The half-reversal approach fails on these cases without explicit handling, as the loop condition x > rev may never be satisfied correctly.