Reverse Integer
Given an integer, return the integer obtained by reversing the digits of the number. If reversing the integer causes it to go out of the range of a 32-bit signed integer (-2^31 to 2^31 - 1), then return 0. Handle negative integers by preserving the negative sign.
[ 123 ]
Explanation. Reversing 123 results in 321.
[ -123 ]
Explanation. Reversing -123 should keep the negative sign, resulting in -321.
[ 120 ]
Explanation. Reversing 120 results in 21 (the leading zero is removed).
[ 1534236469 ]
Explanation. The reverse of 1534236469 is out of the 32-bit signed integer range, hence it returns 0.
Follow-up: Can you implement the solution without converting the integer to a string or using any string operations?
The function must handle integers within the range `-2^31` to `2^31 - 1`. Negative numbers should maintain their sign after the digits have been reversed.
- Views
- 3