DEV Community

Metete Welete
Metete Welete

Posted on • Edited on

Palindrome Number - Leetcode

Palindrome Number

Given an integer x, return true if x is palindrome integer.


Solving this could be as easy as converting the integer to a string and checking if the string is equal to it's reverse.


But there is a statement at the bottom of the question

Follow up: Could you solve it without converting the integer to a string?

This is the approach I used as to task my mind a bit.

So first I checked for possible matches in additions and subtractions but it wasn't providing useful output.

I tried converting to binary to see if I could see patterns but I didn't find any.

Then I used the following steps:

  1. Create an empty array which I would use later to store the reverse integers

  2. Create a loop that would run if the integer was greater than 0

  3. Divide the integer by 10

  4. Store the remainder in the array and have the new integer run through the loop until it gets to 0.

  5. Check if the array is equal to its reverse to return True or False.

This approach stores the remainder of the integer after being divided by 10 which forms the reverse of the integer in the array.

Top comments (0)