class Solution {
public int reverse(int x) {
int flag = 1;
if(x < 0){
flag = -1;
x = x * flag;
}
double maxValue = Math.pow(2,31);
double minValue = 0 - Math.pow(2,31);
int result = 0;
while(true) {
int right = x % 10;//余数
if(result > ((maxValue - 1) - right) / 10){
return 0;
}
if(result < (minValue - right) / 10) {
return 0;
}
result = result * 10 + right;
//if(result > maxValue - 1)此处不能校验住,因为超界后,result是一个小于maxValue - 1的值
x = x / 10;
if(x == 0) {
break;
}
}
if(flag == -1){
result = result * flag;
}
return result;
}
}