输入: 123输出: 321
输入: -123输出: -321
输入: 120输出: 21
注意:
假设我们的环境只能存储 32 位有符号整数,其数值范围是 [−231, 231 − 1]。根据这个假设,如果反转后的整数溢出,则返回 0。
public int reverse(int x) { if (x <= Integer.MIN_VALUE || x >= Integer.MAX_VALUE) { return 0; } int z = 0; int n=0; while(x!=0){ n++; if (n==10){ if (x<0){ if (z<Integer.MIN_VALUE/10){ return 0; } } else{ if (z>Integer.MAX_VALUE/10){ return 0; } } } z = z * 10 + x % 10; x/=10; } return z; }