Given a 32-bit signed integer, reverse digits of an integer.
Example 1:
Input: 123Output: 321
Example 2:
Input: -123Output: -321
Example 3:
Input: 120Output: 21
class Solution { public int reverse(int x) { long tmp = x <= Integer.MIN_VALUE ? 0 : x; long rst = 0, div = 10, tag = x & 0x80000000, val = 0 != tag ? ~tmp + 1 : tmp; while (val / div > 0) { rst = rst * 10 + (val % div) / (div / 10); div *= 10; } rst = rst * 10 + (val % div) / (div / 10); return (int) (0 != tag ? (~rst + 1 < Integer.MIN_VALUE ? 0 : ~rst + 1) : rst < Integer.MAX_VALUE ? rst : 0); }}