输入32位整数,将其反转输出。
Example 1:
Input: 123 Output: 321
Example 2:
Input: -123 Output: -321
Example 3:
Input: 120 Output: 21
题很简单,注意溢出问题即可。
int reverse(int x) {
long long result=0;
int flag = 1;
if (0 > x){
flag = -1;
x *= (-1);
}
while (0<x) {
result = result * 10 + x % 10;
x /= 10;
}
if (result > INT_MAX) result = 0;
return result*flag;
}