Reverse Integer
Reverse digits of an integer.
Example1: x = 123, return 321
Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
注意点需要考虑 反转会溢出的问题;
代码
int reverse(int x)
{
int sum = 0;
while(x)
{
sum = sum * 10 + x % 10;
x = x / 10;
/*-123 % 10 = -3 而不是3,所以不用判断正负号*/
}
return sum;
}