class Solution {
public int reverse(int x) {
long y=x;
long d=0;
int count=0;
int i=0;
while(x!=0) {
x=x/10;
count++;
}
while(y!=0) {
d+=(long) Math.pow(10,count-i-1)*(y%10);
System.out.println(i+"-"+d);
if(d>(long)Integer.MAX_VALUE||d<(long)Integer.MIN_VALUE)
return 0;
y=y/10;
i++;
}
return (int)d;
}
}
这个题目最难就是判断int溢出了,用了各种方法最后想到直接用long来计算不就好了,这样不就轻松判断溢出了。