需要注意边界条件
public int reverse(int x) {
int num = 0;
while (x != 0){
int before = x % 10;
if (num > Integer.MAX_VALUE/10 || (num == Integer.MAX_VALUE/10 && before > 7)){
return 0;
}
if (num < Integer.MIN_VALUE/10 || (num == Integer.MIN_VALUE/10 && before < -8)){
return 0;
}
num = num * 10 + before;
x /= 10;
}
return num;
}