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).
-231 <= x <= 231 - 1The number of digits in x is proportional to log10(x), so converting to a string, reversing it, and converting back each take time.
We create a string representation of x, which requires space to store its digits.
This approach leverages string slicing to reverse the digits without manual digit extraction. We record the sign of x, convert abs(x) to a string, reverse it with [::-1], convert it back to an integer, and reapply the sign. Finally, we check whether the result falls outside the 32-bit signed integer range [-231, 231 - 1] and return 0 if it does.
The while loop iterates once per digit of x, and the number of digits is proportional to log10(x).
We only use a constant number of variables (sign, reversed_x, digit) regardless of the input size.
This approach also builds the reversed number digit by digit using the modulo operator, but performs the overflow check after each digit is appended to reversed_x. In each iteration, we extract the last digit of x, update reversed_x = reversed_x * 10 + digit, and then check whether reversed_x has exceeded the 32-bit signed integer bounds — returning 0 if it has. Finally, we reapply the original sign before returning. This is simpler than the pre-check approach but relies on the language supporting arbitrarily large integers during intermediate computation.
The while loop iterates once per digit of x, and the number of digits is proportional to log10(x).
We only use a constant number of variables (sign, reversed_x, digit, MAX_INT, MIN_INT) regardless of the input size.
This approach builds the reversed number digit by digit using the modulo operator, and critically, checks for potential 32-bit overflow before appending each digit to reversed_x. In each iteration, we extract the last digit of x with x % 10, then verify that multiplying reversed_x by 10 and adding the digit would not exceed MAX_INT or go below MIN_INT — returning 0 immediately if it would. This pre-check ensures we never compute a value outside the 32-bit range, satisfying the problem's constraint of not using 64-bit integers.
| Approach | Rating | Time Complexity | Space Complexity | Advantages | Disadvantages |
|---|---|---|---|---|---|
| String Reversal | Simple and readable | Uses extra space to store the string representation | |||
| Math with Post-overflow Check | Simpler digit-by-digit logic than the pre-check approach | Relies on the language supporting arbitrary-precision integers during intermediate computation | |||
| Math with Pre-overflow Check | Strictly avoids any intermediate overflow, satisfying the no-64-bit constraint | Slightly more complex overflow logic to follow |
The optimal approach for this problem is Solution 3: Math with Pre-overflow Check, which achieves time complexity and space complexity. By verifying the 32-bit bounds before each digit is appended, it fully satisfies the problem's constraint of not using 64-bit integers while remaining memory-efficient.
class Solution: def reverse(self, x: int) -> int: # Determine the sign of x and work with its absolute value sign = 1 if x >= 0 else -1
# Reverse the absolute value of x and apply the original sign reverse_x = int(str(abs(x))[::-1]) * sign
# Check if the reversed integer is within the 32-bit signed integer range if reverse_x < -2**31 or reverse_x > 2**31 - 1: return 0
return reverse_xclass Solution: def reverse(self, x: int) -> int: # Determine the sign of x and work with its absolute value sign = 1 if x >= 0 else -1
x = abs(x) # Work with the absolute value of x for reversal reversed_x = 0
while x != 0: digit = x % 10 # Get the last digit reversed_x = reversed_x * 10 + digit # Append the digit to the reversed number x //= 10 # Remove the last digit from x
# Check if the reversed number is within the 32-bit signed integer range if reversed_x < -2**31 or reversed_x > 2**31 - 1: return 0
# Apply the original sign to the reversed number return reversed_x * sign