Reverse Integer
Given a 32-bit signed integer, reverse the digits of the integer. The function should return 0 if the reversed integer overflows. Consider only scenarios where overflow can occur due to reversing, i.e., the sign should flip only if the number is negative.
[ 123 ]
Explanation. The reverse of 123 is 321.
[ -123 ]
Explanation. The reverse of -123 is -321. Handle negatives by retaining the '-' sign.
[ 120 ]
Explanation. The reverse of 120 is 21, noting that leading zeros are not represented in the final output.
[ 0 ]
Explanation. Reversing 0 results in 0.
[ 1534236469 ]
Explanation. Reversing this number would cause it to overflow, so the output should be 0.
Follow-up: How would your function change if it needed to handle numbers with more digits or higher precision than standard integers?
-2147483648 <= x <= 2147483647 Ensure your function handles negative integers correctly and checks for overflow/underflow.
- Views
- 3