9. Palindrome Number
EasySolutions in Python, Java, C++, TypeScript
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem asks us to determine if a given integer x is a palindrome. A number is a palindrome if it reads the same forwards and backward. The follow-up challenge is to solve this without converting the integer to a string.
Solution
Palindrome Number in Python
class Solution:
def isPalindrome(self, x: int) -> bool:
# Special cases:
# Negative numbers are not palindromes.
# Also, if the last digit is 0, to be a palindrome,
# the first digit must also be 0. Only 0 itself fits this.
if x < 0 or (x % 10 == 0 and x != 0):
return False
reverted_number = 0
while x > reverted_number:
reverted_number = reverted_number * 10 + x % 10
x //= 10
# For numbers with an odd number of digits, the middle digit
# is at the end of reverted_number. We can ignore it.
# e.g., for 12321, at loop end: x = 12, reverted_number = 123.
# So we check x == reverted_number // 10.
# For even digits, x will equal reverted_number.
return x == reverted_number or x == reverted_number // 10
Palindrome Number in Java
class Solution {
public boolean isPalindrome(int x) {
// Special cases:
// Negative numbers are not palindromes.
// If the last digit is 0, to be a palindrome, the first digit must also be 0.
// Only 0 itself satisfies this.
if (x < 0 || (x % 10 == 0 && x != 0)) {
return false;
}
int revertedNumber = 0;
while (x > revertedNumber) {
revertedNumber = revertedNumber * 10 + x % 10;
x /= 10;
}
// For numbers with an odd number of digits, the middle digit is with revertedNumber.
// e.g., for 12321, at loop end: x = 12, revertedNumber = 123.
// We can remove the middle digit by revertedNumber / 10.
// For even digits, x will equal revertedNumber.
return x == revertedNumber || x == revertedNumber / 10;
}
}
Palindrome Number in C++
class Solution {
public:
bool isPalindrome(int x) {
// Special cases:
// As discussed, negative numbers are not palindromes.
// Also if the last digit of the number is 0, in order to be a palindrome,
// the first digit of the number also needs to be 0.
// Only 0 satisfies this property.
if (x < 0 || (x % 10 == 0 && x != 0)) {
return false;
}
int revertedNumber = 0;
while (x > revertedNumber) {
revertedNumber = revertedNumber * 10 + x % 10;
x /= 10;
}
// When the length is an odd number, we can get rid of the middle digit by revertedNumber/10.
// For example when the input is 12321, at the end of the while loop we get x = 12, revertedNumber = 123.
// Since the middle digit doesn't matter in a palindrome (it will always equal to itself), we can simply get rid of it.
// For even length, x will be equal to revertedNumber.
return x == revertedNumber || x == revertedNumber / 10;
}
};
Palindrome Number in TypeScript
function isPalindrome(x: number): boolean {
// Special cases:
// Negative numbers are not palindromes.
// Also, if the last digit is 0, to be a palindrome,
// the first digit must also be 0. Only 0 itself fits this.
if (x < 0 || (x % 10 === 0 && x !== 0)) {
return false;
}
let revertedNumber = 0;
while (x > revertedNumber) {
revertedNumber = revertedNumber * 10 + x % 10;
x = Math.floor(x / 10);
}
// For numbers with an odd number of digits, the middle digit
// is at the end of revertedNumber. We can ignore it by division.
// e.g., for 12321, at loop end: x = 12, revertedNumber = 123.
// So we check x == Math.floor(revertedNumber / 10).
// For even digits, x will equal revertedNumber.
return x === revertedNumber || x === Math.floor(revertedNumber / 10);
};
More Easy LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.