# Why compared with 214748364 in the solution?

• In the solution, the ret value is compared with 214748364, why?

The Integer.MAX_VALUE should be 2147483647 , isn't it?

The solution code is as below:

``````public int reverse(int x) {
int ret = 0;
while (x != 0) {
// handle overflow/underflow
if (Math.abs(ret) > 214748364) {
return 0;
}
ret = ret * 10 + x % 10;
x /= 10;
}
return ret;
}``````

• If you compare with `2147483647`, it is already overflow. Isn't it?

• It's because it is compared before the multiplication by `10`. Any number above `214748364` will yield minimum `2147483650` in value (overflow occurs).

We don't check `Math.abs(ret)==214748364`, because the only acceptable input that will cause this is if the first digit of `x` (last digit of `ret`) is `1`, and that will not cause overflow. (2147483641 <2147483647)

• That's a really magic! Thanks for the answer.

• Thank you! I haven't thought about the only case that makes Math.abs(ret)==214748364 is that the first digit of x is 1.
It's kind of hard to come out with this way to compare.
My solution is set the "ret" to long, and compare with 2147483647 after every "ret = ret * 10 + x % 10;"

• Thank you. It's true. So in my solution I set the "ret" to "long" type and compare it with 2147483647 after every "ret = ret * 10 + x % 10;" calculation.

• To be honest that's what I did too :)

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.