题目:给你一个 32 位的有符号整数 x ,返回将 x 中的数字部分反转后的结果。
如果反转后整数超过 32 位的有符号整数的范围 [−231, 231 − 1] ,就返回 0。
假设环境不允许存储 64 位整数(有符号或无符号)。
package leetcode;
public class five {
public static void main(String[] args) {
int b = -2147483648;
//a(b);
System.out.println(a(b));
}
@SuppressWarnings("unused")
public static int a(int x) {
//率先反转数据
int g = x;
x = (int)Math.abs((double)x);
int e = 0;
//System.out.println(e);
double f = 0;
//int[] d = new int[e];
int[] c = new int[e];
//
//System.out.println((int)Math.abs((double)x));
if(x==0) {
//System.out.println("----------");
g = 0;
e = 0;
}
else {
g = g/Math.abs(g);
e = (int)Math.log10((double)x)+2;
}
System.out.println("e = "+e+","+g+"x="+x);
for (int i = 1; i < e; i++) {
f += ((int)(x%Math.pow(10, i)/Math.pow(10, i-1))*Math.pow(10, e-i-1));
//System.out.println("c["+i+"] = "+c[i]);
}
// int c = x%Math.pow(10,);
if(f>=Math.pow(2, 31)-1||f<=-Math.pow(2, 31)) {
return 0;
}
else {
//System.out.println(f);
return (int)(f*g);
}
}
}