Reverse Integer

easynumber theory

Given a signed 32-bit integer $$$x$$$, return $$$x$$$ with its digits reversed.

If reversing $$$x$$$ causes the value to go outside the signed 32-bit integer range $$$[\text{-}2^{31}, 2^{31} - 1]$$$, then return $$$0$$$.

Note: Assume the environment does not allow you to store 64-bit integers (signed or unsigned).

Input Format:

The input consists of a single line containing a signed 32-bit integer $$$x$$$.

$$$-2^{31} \le x \le 2^{31} - 1$$$

Output Format:

Print the reversed integer. If the reversed integer overflows, print $$$0$$$.

Examples:

Example 1:

Input:

123

Output:

321

Example 2:

Input:

-123

Output:

-321

Example 3:

Input:

1200

Output:

21

Note:

Test case 1: Input $$$123$$$. Reversing the digits gives $$$321$$$, which is within the $$$[-2^{31}, 2^{31}-1]$$$ range.

Test case 2: Input $$$-123$$$. Reversing the digits gives $$$-321$$$, which is still within the valid range.

Test case 3: Input $$$1200$$$. Reversing the digits gives $$$0021$$$, which is interpreted as $$$21$$$.

Code

Loading Editor...