Solving the LeetCode Problem Palindrome Number
Intro:
For this blog I’ll be solving another popular algorithm problem from LeetCode, Palindrome Number. Note that this problem will be solved in JavaScript! I’ll also be demonstrating three ways in which we can solve this problem.
The question is asking us to return true
if the number is a palindrome. A palindrome is a number that when you reverse it, it’s still the same number. By going over the examples, I also realized that if the input is a negative number (-101) then the boolean value in return should be false
. So let’s begin!
Using Built-In Methods:
Here are the built-in methods that we can use to solve the first step which is reversing the number:
In the picture above, I created a variable newNum
that’s going to represent the reversed number. I used .toString()
to convert num
into a string. I then used .split(‘’)
to convert the string into an array of strings. After that, I used .reverse()
to reverse the elements inside of the array. Finally, I used .join(‘’)
to convert all of the elements inside of the array into a string (the opposite of what .split(‘’)
does). For more info on these built in methods, make sure to check out the resources section below!
Although newNum
is now a reversed version of num
, newNum
is still a string. Since the input needs to be an integer, I decided to create a variable parsedNumber
that converts newNum
into an integer. Since the question is only asking for a boolean value, we can compare parsedNumber
and num
by using a triple equal sign (===
). Here’s the solution:
There’s another way we can solve this without having to convert newNum
into an integer. We can also have a shorter solution like this:
The only difference is that instead of converting newNum
into an integer, I compared both num
and newNum
using a double equal sign (==
). For more info on double equal signs, make sure to check out the resources section below!
The problem with these two solutions is that we may not know the time and space complexities of these built-in methods. It’s great how the solution is only two lines of code, but that’s not always the right way! I believe it’s also great to show that you can solve these problems without using built-in methods. That’s why I’ll also show one more solution that doesn’t use built-in methods.
Not Built-In Methods:
I’m aware that I need to have a variable that represents the reversed number. So I created a variable reversed
that at the moment will be equal to zero. Similar to the solution with the built-in methods, I would like to also compare reversed
with another number so that I can get a boolean value. That’s why I created a variable original
which will be equal to num
.
After this step, we need to create some form of loop that would iterate over num
and also reverse it. For this example, I’m going to use a while
loop! Here’s what the code looks like so far:
On line 34
, I’m changing the value of reversed
and now setting it equal to 0 * 10 + num % 10
. I placed the zero because that’s what reversed
is at the moment (line 29
). Since this is happening inside of a while
loop and num
is currently -11, the value of reversed
will be dependent on the input of our choice. For example, since I’m using -11 as my input, this is how line 34
really looks like, 0 * 10 + (-11) % 10
. reversed
is now equal to -1. It doesn’t stop there because num
is now -1 which means that the loop will continue. So this is what line 34
looks like now, -1 * 10 + (-1) % 10
and reversed
is now equal to -11. Line 39
is making sure that num
equals to 0 when the while
loop finishes reversing num
. In this case, reversed
and the input (-11) are equal to each which means that the boolean value that’s going to be returned should be true
. As mentioned in the beginning of this blog, negative numbers should return false
! One way to fix this is by creating an if
statement that returns false
if reversed
is a negative number. Here’s the full solution:
I hope this blog was helpful! Please feel free to reach out or leave a comment on this blog if there are better solutions for this problem!