注意溢出情况返回0。
class Solution {
public:
int reverse(int x) {
const int max=0x7fffffff;
const int min=0x80000000;
long long res = 0;
while(x)
{
res = res*10 + x%10;
if(res>max||res<min)
return 0;
x /= 10;
}
return (int)res;
}
};