LeetCode #9 – Palindrome Number

This is the second question I have completed:

Here are examples of output for this problem:

I would also like to note that there is a follow-up question to this question, which is:

My solution will already be without converting the integer to a string, so this follow-up question will already be addressed. The main reason why I didn’t convert the integer to a string is because I thought it would be too inefficient.

My logic for this problem was to get the original number, and reverse it. At the end, I would compare the reversed number to the original number. Here is my solution:

The first 2 lines with the if statement are to make sure that the number is not negative, because if the number is negative, it is not a palindrome.

The while statement is reversing the original number by taking the last digit of the original number and adding it to the front of the reverse. 

The final if statement is to just check if the original is the same as the reversed number, if it is, then it returns true, and if it isn’t, it returns false.

Here is the output:

Here are the logistics of my solution:

That is all from me for now. I hope you guys at least learned something today, and that you all have a great day!

By:

Posted in:


Leave a comment