# int32bit / leetcode

Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
..
Failed to load latest commit information.
solve.c

## 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.

## Solution

1. 最低位容易`low = x % 10 = 1234321 % 10 = 1`;
2. 最高位就是除以N = 10n-1,n为x的位数,等于7, 即`high = x / 1000000 = 1`;
3. 显然若`low != high` 返回`false`，否则`low == high`, 此时要把x的最高位和最低位去掉，得到23432.方法是：去掉最高位x %= N, 然后去掉最低位 x /= 10.
4. 回到1,直到x=0.

## Code

```bool isPalindrome(int x)
{
if (x < 0)
return false;
int len = 1;
for (; x / len >= 10; len *= 10);
while (x) {
int high = x / len;
int low = x % 10;
if (low != high)
return false;
x = (x % len) / 10;
len /= 100;
}
return true;
}```
You can’t perform that action at this time.