Reverse Integer
Given a 32-bit signed integer, reverse its digits. Write a function that returns the reversed integer. If the reversed integer overflows (i.e., exceeds the signed 32-bit integer range [-2^31, 2^31 - 1]), the function should return 0.
[ 123 ]
Explanation. Reversing the digits of 123 results in 321.
[ -123 ]
Explanation. Reversing the digits of -123 results in -321.
[ 120 ]
Explanation. Reversing the digits of 120 (which has a trailing zero) results in 21.
[ 1534236469 ]
Explanation. Reversal of 1534236469 overflows a 32-bit integer, hence returns 0.
Follow-up: Can this implementation be optimized further? Are there any edge cases that the provided solution does not cover?
The function should handle both positive and negative inputs.
- Views
- 2