给定一个32位有符号整数,整数的反转数字。
例1:
输入: 123 输出: 321
例2:
输入: -123 输出: -321
例3:
输入: 120
输出: 21
public static int reverse1(int x) {
long result=0;
while(x!=0)
{
result=(result*10)+(x%10);
if(result>Integer.MAX_VALUE)return 0;
if(result>Integer.MIN_VALUE)return 0;
x=x/10;
}
// System.out.println(result);
return (int) result;
}
原先用了数组的方法,发现论坛里面这个更简单。