Reverse Integer
Given a signed 32-bit integer x, return x with its digits reversed. The sign is preserved, so reversing a negative number stays negative.
If reversing x produces a value that falls outside the signed 32-bit range [-2^31, 2^31 - 1], return 0 instead. Solve it without relying on 64-bit integer support.
Example cases
- positivein x = 123out 321Reversing the digits of 123 gives 321.
- negativein x = -123out -321The sign is kept, so -123 reverses to -321.
- trailing zeroin x = 120out 21Reversing 120 gives 021, and leading zeros are dropped to 21.
- overflowin x = 1534236469out 0Reversed this is 9646324351, which exceeds 2^31 - 1, so the result is 0.
Constraints
- -2^31 <= x <= 2^31 - 1
x =
123