用long来保存,判断是否超过intmax 和intmin。
class Solution {
public:
int reverse(int x) {
bool negative = x<0?true:false;
long lx = x;
if(negative){
lx = -lx;
}
long ret = 0;
do{
ret*=10;
ret+=lx%10;
lx/=10;
}while(lx!=0);
if(negative){
ret = -ret;
}
if(ret > ~(1<<31) || ret<(1<<31)){
return 0;
}
return ret;
}
};