Palindrome Number

Posted on

screen-shot-2016-09-24-at-5-20-07-pm

Problem

Determine whether an integer is a palindrome. Do this without extra space.

click to show spoilers.

Some hints:Could negative integers be palindromes? (ie, -1)

If you are thinking of converting the integer to string, note the restriction of using extra space.

You could also try reversing an integer. However, if you have solved the problem “Reverse Integer”, you know that the reversed integer might overflow. How would you handle such case?

There is a more generic way of solving this problem.

Solution

The inversed version of palindrome numbers are the same as themselves. So inverse the number first. For the digit manipulations of the integers, use % and / to get every digit of an integer.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s