Given a 32-bit signed integer, invert the numbers in the integer.
Considering here, when exchanging, you need to pay attention to the different digits, you need to multiply the corresponding digits, the code is as follows:
int reverse(int x)
{
int result = 0;
while(x != 0)
{
int tmp = result * 10 + x % 10; //转换的中间值
x = x / 10; //每循环一次,x位数减少一位
if(tmp / 10 != result) //验证数据是否正确
{
result = 0;
break;
}
result = tmp;
}
return result;
}