水了水了,时间不够,勉强凑出一版。
(长度为32的数组是来搞笑的,嗯,没错)
public int reverse(int x) {
int[] arr = new int[32];
long x1 = x;
int sign = x1 < 0 ? -1 : 1;
x1*=sign;
int i = 0;
do{
arr[i++] = (int)x1%10;
x1/=10;
}while(x1>=1);
long y = 0;
for(int j=1;j<=i;j++){
y+= arr[j-1]*Math.pow(10,i-j);
if(y*sign>Integer.MAX_VALUE || y*sign<Integer.MIN_VALUE){
return 0;
}
}
y*=sign;
return (int)y;
}
待解决
int如何处理溢出的