解题思路:主要是溢出情况的判断,在乘10之前判断当前值是否大于最大整数除10或者小于最小整数除10,如果满足条件就会溢出,按题意返回0。
public class Solution {
public int reverse(int x) {
int res = 0;
while(x != 0){
if(res > Integer.MAX_VALUE/10 || res < Integer.MIN_VALUE/10) {
return 0;
}
res = res*10 + x%10;
x /= 10;
}
return res;
}
}