91. Reverse Integer

Easy · Math

Given a signed 32-bit integer, reverse its digits. If the reversed integer overflows the signed 32-bit integer range [-2^31, 2^31 - 1], return 0.

A signed 32-bit integer ranges from -2,147,483,648 to 2,147,483,647. After reversing, if the result exceeds these bounds, the function should return 0 instead.

Examples

Example 1
Input: 123
Output: 321
Explanation: Reversing the digits of 123 gives 321, which is within the 32-bit signed integer range.
Example 2
Input: -456
Output: -654
Explanation: Reversing -456 preserves the negative sign and reverses the digits to get -654.

Constraints