leetcode 007
public static int reverse(int x) {
// x < 0 ?
boolean isLtZero = (x >>> 31 & 1) == 1;
// 将所有正数 当做负数处理了
x = isLtZero ? x : -x;
int limit1 = Integer.MIN_VALUE / 10;
int limit2 = Integer.MIN_VALUE % 10;
int res = 0;
while (x != 0) {
// 1. 这个数已经 比 最小值除以10 还小了,
// 2. 这个数已经是系统最小值除以10了,如果加上的余数 比 limit2 还小
// 满足以上两个条件中任意一个 一定越界 直接返回 0
if (res < limit1 || (res == limit1 && x % 10 < limit2)) {
return 0;
}
res = res * 10 + x % 10;
x = x / 10;
}
return isLtZero ? res : Math.abs(res);
}