Hey everyone! Mahdi Shamlou here — back with another LeetCode classic problem 🚀
After #8 String to Integer (atoi), today I solved Problem #9 — Palindrome Number.
At first, I thought:
"This should be easy... just check whether the number reads the same from left to right and right to left."
But instead of reversing the number, I decided to treat it like a palindrome string problem and expand from the center.
Problem Statement
Given an integer x, return true if x is a palindrome, and false otherwise.
A palindrome number reads the same backward as forward.
Examples
Input: 121
Output: true
Input: -121
Output: false
Input: 10
Output: false
Input: 1221
Output: true
My Thought Process
When I started, this was my idea:
- Change
xfrom integer to string. - Find the center of the string.
- Expand around the center.
- Compare characters on both sides.
- If everything matches until the edges, return
True.
My initial notes were literally:
- first of all i think change x from int to str
- secend i think i can find center of str and expand around the center with consider odd or even x_str
- three if any one of them is not equal we can return False else return True
So that's exactly the approach I implemented.
My Solution
class Solution:
def isPalindrome(self, x: int) -> bool:
"""
first of all i think change x from int to str
secend i think i can find center of str and expand around the center with consider odd or even x_str
three if any one of them is not equal we can return False else return True
"""
x_str = str(x)
x_is_palindrome = False
print(f"x : {x_str} and type : {type(x_str)}")
if len(x_str) % 2 == 0:
print(f"x is even and len is : {len(x_str)}")
mid_of_str_num = len(x_str) / 2
left = mid_of_str_num - 1
right = mid_of_str_num
print(f"left for start is : {left}")
print(f"right for start is : {right}")
while left >= 0 and right < len(x_str) and x_str[int(left)] == x_str[int(right)]:
left -= 1
right += 1
left = left + 1
right = right - 1
print(f"left in end is : {left}")
print(f"right in end is : {right}")
return (left == 0) and (right == len(x_str)-1)
else:
print(f"x is odd and len is : {len(x_str)}")
mid_of_str_num = int(len(x_str) / 2)
left = mid_of_str_num
right = mid_of_str_num
print(f"left for start is : {left}")
print(f"right for start is : {right}")
while left >= 0 and right < len(x_str) and x_str[int(left)] == x_str[int(right)]:
left -= 1
right += 1
left = left + 1
right = right - 1
print(f"left in end is : {left}")
print(f"right in end is : {right}")
return (left == 0) and (right == len(x_str)-1)
return False
x = Solution()
res = x.isPalindrome(x=121)
print(res)
How It Works
For Odd Length Numbers
Example:
121
The center is:
2
Start with:
left = center
right = center
Then expand outward:
1 2 1
^ ^
If both sides keep matching until we reach the edges, the number is a palindrome.
For Even Length Numbers
Example:
1221
There is no single center.
Instead we start from:
12|21
^
and compare:
1 2 2 1
^ ^
Then expand:
1 2 2 1
^ ^
If every comparison matches, we return True.
What I Learned
This problem reminded me of a common pattern:
Expand Around Center
I usually think about this technique for string palindrome problems, but it also works nicely here after converting the integer into a string.
Another thing I noticed is that handling odd-length and even-length cases separately makes the logic easier to understand.
Complexity Analysis
Time Complexity
O(n)
We may visit each character once while expanding from the center.
Space Complexity
O(n)
Because we convert the integer into a string.
Final Thoughts
This problem wasn't really about a complicated algorithm.
For me, it was about taking a familiar palindrome technique and applying it to numbers.
I know there are other solutions that avoid converting to a string, but I wanted to solve it using the first idea that came to my mind and make it work correctly.
And that's one thing I enjoy about LeetCode:
Sometimes the best solution is not the first accepted solution. But every accepted solution teaches you something.
What About You?
Did you solve this problem using:
- String conversion?
- Reversing the integer?
- Two pointers?
- Another approach?
Drop your thoughts — I read everything! 🚀
My Repo (All Solutions)
GitHub — mahdi0shamlou/LeetCode
Want More?
If you enjoyed this deep dive, check out my other articles:
- Design Patterns in Programming: Stop Building Spaghetti Code (2026 Edition) | Mahdi Shamlou — Design Patterns
- Message Brokers in 2026: Kafka, RabbitMQ, NATS — Which One Should You Really Use — Architecture decisions
- OWASP Top 10 for Developers (2026 Edition) — How to Actually Fix the Most Dangerous Web Vulnerabilities — Security patterns
- Injection Attacks Are Not Dead: SQL, NoSQL, ORM, and Command Injection — How to Actually Fix Them — Attack patterns
- Durable Workflow Engines: Temporal vs dbt OS vs Transact vs Prefect — System patterns
🔗 LinkedIn: https://www.linkedin.com/in/mahdi-shamlou-3b52b8278
📱 Telegram: https://telegram.me/mahdi0shamlou
📸 Instagram: https://www.instagram.com/mahdi0shamlou/
Author: Mahdi Shamlou | مهدی شاملو


Top comments (0)