# Concise C++ solution, maybe?

• The import thing is to remember the overflow condition. The follow solution cost 14ms.

``````int reverse(int x) {
int reverse = 0;
int MAX_INT = 0x7fffffff;

while(x)
{
reverse = reverse * 10 + x % 10;
x = x /10;
if((reverse > 0 ? reverse : -reverse) > (MAX_INT / 10) && x)
{
return 0;
}
}

return reverse;
}``````

• Do you know why the machine says this? I think I considered the overflow situation

Input: 1534236469

Output: 1056389759

Expected: 0

int a = x;
int temp=0;
int sign=1;

``````    if(a<=0)
{
a=-a;
sign=-1;
}

while(a>0)
{
temp=10*temp+a%10;
a=a/10;
}
if(temp> INT_MAX) return 0;

return sign*temp;``````

• as tested, by print temp in the while loop we have:

9
96
964
9646
96463
964632
9646324
96463243
964632435 //should stop here as next loop will excess limit
1056389759
1056389759

9646324351 excess the limit of 2147483647(32-bit signed int limit)

use long int type for temp will avoid this problem.

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