Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
代码如下:
class Solution {
public:
int reverse(int x) {
if (x == -x)
{
return 0;
}
if (x < 0)
{
return -reverse(-x);
}
int result = 0;
int cutoff = INT_MAX;
int cutlim = cutoff %10;
cutoff = cutoff /10;
while (x)
{
int tmp = x%10;
result = 10 * result ;
result += tmp;
x = x/10;
if (result>cutoff && x)
{
result = 0;
break;
}
}
return result;
}
};