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

## Reverse Integer

Reverse digits of an integer.

``````Example1: x = 123, return 321
Example2: x = -123, return -321
``````

click to show spoilers.

Here are some good questions to ask before coding. Bonus points for you if you have already thought through this!

• If the integer's last digit is 0, what should the output be? ie, cases such as 10, 100.

• Did you notice that the reversed integer might overflow? Assume the input is a 32-bit integer, then the reverse of 1000000003 overflows. How should you handle such cases?

For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.

Update (2014-11-10): Test cases had been added to test the overflow behavior.

## Solution

```int reverse(int x)
{
int sign = 0, ans = 0;
if (x < 0) {
x = -x;
sign = 1;
}
while (x) {
ans *= 10;
ans += x % 10;
x /= 10;
}
return sign ? -ans : ans;
}```

• 如果是负数, 最小值为-2147483648
• 如果是正数, 最大值是2147483647
• `ans > INT_MAX / 10`，则一定会溢出
• `ans == INT_MAX / 10`, 若`n < 0, 且 x <= 8`,不会溢出。`n > 0 && x <= 7`不会溢出.否则也会溢出

## Code

```int reverse(int x)
{
int sign = 0, ans = 0;
const int BOUND = INT_MAX / 10;
if (x == -2147483648)
return 0;
if (x < 0) {
x = -x;
sign = 1;
}
while (x) {
ans *= 10;
ans += x % 10;
x /= 10;
if (ans >= BOUND)
break;
}
if (x == 0)
return sign ? -ans : ans;
if (ans > BOUND) // 此时乘以10,无论如何都要溢出
return 0;
if (sign && x == 8) // 刚好是最大的负数，其实这种情况不可能出现
return -2147483648;
if (x > 7) // ans已经等于INT_MAX / 10，如果个位大于7溢出.
return 0;
ans *= 10;
ans += x; // 此时一定是2147483641
return sign ? -ans : ans;
}```
You can’t perform that action at this time.