LeetCode #9: Palindrome Number — Solved in Java
Convert to String and Use Two Pointers and Reverse Half of the Number
Given an integer, the goal is to figure out if it reads the same forward and backward. It's the usual palindrome check, but done with numbers instead of strings.
The first version uses string conversion and compares characters from both ends. After that, we’ll walk through a version that sticks to digits and math only. Both are broken down step by step, and the final code is ready to copy and run.
Given an integer
x
, returntrue
ifx
is a palindrome, andfalse
otherwise.Example 1:
Input: x = 121
Output: true
Explanation: 121 reads as 121 from left to right and from right to left.
Example 2:
Input: x = -121
Output: false
Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.
Example 3:
Input: x = 10
Output: false
Explanation: Reads 01 from right to left. Therefore it is not a palindrome.
Constraints:
-231 <= x <= 231 - 1
Solution 1 — Convert to String and Use Two Pointers
This solution leans on basic string handling. The number is first converted into a string, which lets us use character indexing to check the digits. We compare the characters from both ends of the string, moving toward the center. If every character lines up, it's a palindrome. If anything breaks that symmetry, we stop early.
Let’s break it down:
class Solution {
public boolean isPalindrome(int x) {
This method takes in an integer and returns true
if it’s a palindrome or false
if it’s not.
if (x < 0) return false;
Negative numbers always return false
. The negative sign throws off the left-to-right and right-to-left check, and there’s no matching closing -
on the other side.
String s = Integer.toString(x);
int left = 0, right = s.length() - 1;
The number is turned into a string so we can look at each character. We use two pointers to track both ends. left
starts at the first character, and right
starts at the last one.
while (left < right) {
if (s.charAt(left) != s.charAt(right)) return false;
left++;
right--;
}
We walk the two pointers inward one step at a time. At each step, we check if the characters match. If they don’t, then the number doesn’t read the same both ways, and we return false
.
return true;
}
}
If the loop finishes without returning, that means every character matched its counterpart. We return true
at the end.
This scans the entire string once, so it runs in O(n) time, where n
is the number of digits. Space is also O(n) because of the string conversion. It's useful for quick checks and works well when you don’t have space constraints.
Reverse Half of the Number — Copy and Paste
This version keeps everything numeric and avoids turning the number into a string. Instead, we reverse only the second half of the number, digit by digit, and then compare it to the first half. This way, we don’t need to reverse the whole thing, and we also avoid issues with integer overflow.
Let’s go through the logic:
class Solution {
public boolean isPalindrome(int x) {
We’re checking if a given integer reads the same backward as forward, using only math.
if (x < 0 || (x % 10 == 0 && x != 0)) return false;
Negative numbers aren’t palindromes. Also, if the number ends in zero but isn’t zero itself, then it can’t be a palindrome. There’s no leading zero on the reverse side to match it.
int reversed = 0;
while (x > reversed) {
int digit = x % 10;
reversed = reversed * 10 + digit;
x = x / 10;
}
We extract digits from the right side of the number and build the reverse. The loop stops when the reversed number has caught up to or passed the remaining part of the original number. At that point, we’ve processed half the digits.
return (x == reversed || x == reversed / 10);
}
}
For numbers with an even number of digits, the two halves match exactly. For odd digit counts, the reversed number will have an extra digit at the end, which we drop using integer division by 10.
This method uses O(1) space and runs in O(log n) time because we’re only working with half the digits. It's more efficient and clean once you're comfortable with number operations.
Convert to String and Use Two Pointers — Copy and Paste
class Solution {
public boolean isPalindrome(int x) {
if (x < 0) return false;
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;
}
}
Reverse Half of the Number — Copy and Paste
class Solution {
public boolean isPalindrome(int x) {
if (x < 0 || (x % 10 == 0 && x != 0)) return false;
int reversed = 0;
while (x > reversed) {
int digit = x % 10;
reversed = reversed * 10 + digit;
x = x / 10;
}
return (x == reversed || x == reversed / 10);
}
}