1. Problem Statement (Simple Explanation):
You are given an integer x.
Return:
true if x is a palindrome.
false otherwise.
A palindrome reads the same forward and backward.
2. Examples:
Example 1:
Input: x = 121
Output: true
Explanation:
121 reads the same from left to right and right to left.
Example 2:
Input: x = -121
Output: false
Explanation:
Left to right: -121
Right to left: 121-
Not the same → not a palindrome.
Example 3:
Input: x = 10
Output: false
Explanation:
Reversed digits → 01, which is 1, not equal to 10.
Constraints:
-231 ≤ x ≤ 231-1
Follow-up: Solve without converting the integer to a string.
3. Approach 1 – String Conversion (Straightforward, But Not Follow-up):
Intuition:
Convert the integer to a string.
Check if the string is equal to its reverse.
Edge cases:
Negative numbers automatically not palindromes (because of '-' sign).
Algorithm (String-based):
If x < 0, return false.
Convert x to string s.
Check if s == reverse(s).
Return that boolean.
Pseudo-code (String Approach):
Complexity:
Let k be number of digits:
Time: O(k)
Space: O(k) for string.
Simple, but doesn’t meet the follow-up requirement.
4. Approach 2 – Reverse the Whole Number (No String, But Risky for Overflow):
We can reuse logic similar to “Reverse Integer”:
Reverse the entire integer.
Compare reversed value with original.
But reversing the full integer can overflow a 32-bit integer, and you’d need to handle that carefully.
There is a better integer-only method that avoids overflow and extra work: reverse half the number.
5. Approach 3 – Reverse Half the Digits (Optimal, Integer-only):
This is the standard follow-up solution.
Key Ideas:
Negative numbers are not palindromes (due to '-').
Any positive number ending with 0 (and not equal to 0 itself) cannot be a palindrome:
Example: 10, 100, 120 etc.
Instead of reversing the entire number, we reverse only the last half of the digits and compare with the first half.
How Half-Reversal Works:
Let x be non-negative and not ending with zero (unless it is zero):
We maintain reversedHalf:
Repeatedly:
Take last digit of x (via % 10) and add it to reversedHalf.
Remove last digit from x (via / 10).
We stop when reversedHalf >= x.
At that point:
For even number of digits:
x == reversedHalf if palindrome.
For odd number of digits:
Middle digit is in reversedHalf (e.g., 12321 → eventually x = 12, reversedHalf = 123).
We can remove the middle digit from reversedHalf by integer division by 10.
Then check x == reversedHalf / 10.
Algorithm (Step-by-Step):
If x < 0: return false.
If x != 0 and x % 10 == 0: return false.
Numbers like 10, 100, 120 cannot be palindromes.
Initialize reversedHalf = 0.
While x > reversedHalf:
digit = x % 10
reversedHalf = reversedHalf * 10 + digit
x = x / 10 (integer division)
After loop:
If x == reversedHalf → even number of digits → palindrome.
Else if x == reversedHalf / 10 → odd number of digits → palindrome.
Else → not a palindrome.
Pseudo-code (Half Reversal, Recommended):
Complexity:
Let k be number of digits:
Time: O(k)
Space: O(1)
We reverse only half the digits, so it’s safe from overflow and efficient.
6. Java code:
7. C code:
8. C++ code:
9. Python code:
10. JavaScript code:







Comments
Post a Comment