October 29, 2020
Hello happy people 👋! Today we will look into a fairly easy LeetCode problem
Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward.
Follow up: Could you solve it without converting the integer to a string?
-231 <= x <= 231 - 1
Example 1:
Input: x = 121
Output: true
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.
Example 4:
Input: x = -101
Output: false
We need to find if a give number is palindrome or not. A palindrome number is one which when reversed gives the same number. For e.g, 747 is Palindrome number and 526 is not because when reversed it gives 625 which is not equal to 526.
Also, as it is evident from the examples that negative numbers are not palindromes because they start with -
and when reversed the -
sign will come at the end which is invalid. Therefore, we only have to check for positive numbers
We can easily solve this problem by reversing the given number and comparing the reversed number with the given number.
false
, else proceed to #2.x
in a variable number
. We are doing it because we will perform our operations on number
and due to that, it’s value will change. We will use x
at the end of the program to compare with the reversed number.true
if the reverse number and given number are equal, false
otherwise.Since we are going through the entire number digit by digit, the time complexity should be O(log10n). The reason behind log10 is because we are dealing with integers which are base 10.
We are not using any data structure for interim operations, therefore, the space complexity is O(1).
Let’s get our hands dirty with code 😍.
public class PalindromeNumber {
public boolean isPalindrome(int x) {
// Base condition
if (x < 0) {
return false;
}
// Store the number in a variable
int number = x;
// This will store the reverse of the number
int reverse = 0;
while (number > 0) {
reverse = reverse * 10 + number % 10;
number /= 10;
}
return x == reverse;
}
}
def isPalindrome(x: int) -> bool:
# Base condition
if x < 0:
return False
# Store the number in a variable
number = x
# This will store the reverse of the number
reverse = 0
while number:
reverse = reverse * 10 + number % 10
number //= 10
return x == reverse
var isPalindrome = function(x) {
// Base condition
if (x < 0) {
return false;
}
// Store the number in a variable
let number = x;
// This will store the reverse of the number
let reverse = 0;
while (number > 0) {
reverse = reverse * 10 + number % 10;
number = parseInt(number / 10);
}
return x === reverse;
};
fun isPalindrome(x: Int): Boolean {
// Base condition
if (x < 0) {
return false
}
// Store the number in a variable
var number = x
// This will store the reverse of the number
var reverse = 0
while (number > 0) {
reverse = reverse * 10 + number % 10
number /= 10
}
return x == reverse
}
Congratulations 👏!!! We’ve solved yet another problem from LeetCode and it didn’t take too much of our sweat, did it?
I hope you have enjoyed this post. Feel free to share your thoughts on this.
You can find the complete source code on my GitHub repository. If you like what you learn. feel free to fork 🔪 and star ⭐ it.
Till next time… Happy coding 😄 and Namaste 🙏!