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 [-231, 231 - 1], then return 0.
Assume the environment does not allow you to store 64-bit integers (signed or unsigned).
Input: x = 123
Output: 321
Input: x = -123
Output: -321
Input: x = 120
Output: 21
Constraints:
This simple approach involves converting the integer to a string, reversing it and converting it back to an integer. While naive, it's straightforward to implement and understand.
Instead of using built-in string manipulation, this approach repeatedly divides the input by 10 to extract digits and form the reversed integer.
Handling overflow is crucial because the problem constraints specify that the reversed integer must fit within a 32-bit signed integer range. This can be efficiently achieved by checking possible overflow conditions before updating the result in each iteration.