解题思路:主要是溢出情况的判断,在乘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;
}
}
本文介绍了一种实现整数反转的方法,并考虑了在反转过程中可能出现的整数溢出问题。通过检查当前值是否超过最大或最小整数除以10来避免溢出,一旦发生溢出则返回0。
469

被折叠的 条评论
为什么被折叠?



