Intermediate
Open
Pro
Reverse 32-Bit Integer
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 [-2^31, 2^31 - 1], return 0
instead.
Assume the environment does not allow storing 64-bit (or larger) integers, so you must detect overflow before (or as) it happens rather than checking the final value after the fact.
Example 1
Input: x = 123
Output: 321
Example 2
Input: x = -123
Output: -321
Example 3
Input: x = 120
Output: 21
Explanation: Leading zeros in the reversed number are dropped.
Example 4
Input: x = 1534236469
Output: 0
Explanation: The reversed value, 9646324351, exceeds 2^31 - 1, so
the function returns 0.
Constraints
-2^31 <= x <= 2^31 - 1
Share this question