题目描述
因为数字反转之后可能会超出int的范围,但是又不可以采用long长整型存储,所以需要在数字转换的过程中对其进行操作
主要思想
代码
class Solution {
public int reverse(int x) {
int a=Integer.MAX_VALUE;
int b=Integer.MIN_VALUE;
int res=0;
while(x!=0){
int y=x%10;
if(x>0 && res > (a-y)/10 ) return 0;
if(x<0 && res < (b-y)/10 ) return 0;
res=res*10+y;
x=x/10;
}
return res;
}
}