## 9. Palindrome Number

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.

b'
\n
\n\n
\n

## Solution

\n
\n

#### Approach #1 Revert half of the number [Accepted]

\n

Intuition

\n

The first idea that comes to mind is to convert the number into string, and check if the string is a palindrome, but\nthis would require extra non-constant space for creating the string which is not allowed by the problem description.

\n

Second idea would be reverting the number itself, and then compare the number with original number, \nif they are the same, then the number is a palindrome. However, if the reversed number is larger than , \nwe will hit integer overflow problem.

\n

Following the thoughts based on the second idea, to avoid the overflow issue of the reverted number, what if we only \nrevert half of the number? After all, the reverse of the last half of the palindrome should be the same as the \nfirst half of the number, if the number is a palindrome.

\n

For example, if the input is 1221, if we can revert the last part of the number "1221" from "21" to "12",\nand compare it with the first half of the number "12", since 12 is the same as 12, we know that the number is a palindrome.

\n

Let\'s see how we could translate this idea into an algorithm.

\n

Algorithm

\n

First of all we should take care of some edge cases. All negative numbers are not palindrome, for example: -123 is \nnot a palindrome since the \'-\' does not equal to \'3\'. So we can return false for all negative numbers.

\n

Now let\'s think about how to revert the last half of the number. For number 1221, if we do 1221 % 10, we get the \nlast digit 1, to get the second to the last digit, we need to remove the last digit from 1221, we could do so by \ndividing it by 10, 1221 / 10 = 122. Then we can get the last digit again by doing a modulus by 10, 122 % 10 = 2, and if we multiply the last digit by 10 and add the second last digit, 1 * 10 + 2 = 12, it gives us the reverted number we want. Continuing this process would give us the reverted number with more digits.

\n

Now the question is, how do we know that we\'ve reached the half of the number?

\n

Since we divided the number by 10, and multiplied the reversed number by 10, when the original number is less than the \nreversed number, it means we\'ve processed half of the number digits.

\n\n

Complexity Analysis

\n
\n
• \n

Time complexity : .\nWe divided the input by 10 for every iteration, so the time complexity is \n

\n
• \n
• \n

Space complexity : .

\n
• \n
\n
\n

Analysis written by: @ccwei

\n
'