7. Reverse Integer
Intuition
The key idea is to extract each digit from the input number from right to left and build the reversed number. We need to be careful about integer overflow since the problem requires handling 32-bit integer constraints.
Approach
- Initialize a variable
ans
to store the reversed number - While the input number
x
is not zero:- Get the last digit using modulo operation (
x % 10
) - Remove the last digit from x by integer division (
x /= 10
) - Build the reversed number by multiplying current ans by 10 and adding the new digit
- Check for 32-bit integer overflow
- Get the last digit using modulo operation (
- Return the reversed number if no overflow occurred
Complexity
- Time complexity: O(log(x))
- Space complexity: O(1)
Keywords
- Math
- Integer Overflow
- Modulo Operation
- 32-bit Integer Constraints