Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
唯一难点在于处理overflow,计算之前提前判断一下是否会溢出即可
int reverse(int x) {
int res=0;
const int max_inf=0x7fffffff;
const int min_inf=0x80000000;
while(x){
if(res>max_inf/10 || (res==max_inf/10 && x%10>max_inf%10))return 0;
if(res<min_inf/10 || (res==min_inf/10 && x%10<min_inf%10))return 0;
res*=10;
res+=x%10;
x/=10;
}
return res;
}