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).
Example 1:
Input: x = 123 Output: 321
Example 2:
Input: x = -123 Output: -321
Example 3:
Input: x = 120 Output: 21
Constraints:
-231 <= x <= 231 - 1
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force way to reverse an integer is like doing it manually. We'll look at the number digit by digit and build a new reversed number. We need to also handle any negative signs.
Here's how the algorithm would work step-by-step:
def reverse_integer(integer_to_reverse):
is_negative = integer_to_reverse < 0
# Convert to positive to simplify the reversing process
if is_negative:
integer_to_reverse = -integer_to_reverse
reversed_integer = 0
while integer_to_reverse > 0:
# Extract the last digit
last_digit = integer_to_reverse % 10
# Build the reversed integer
reversed_integer = (reversed_integer * 10) + last_digit
# Remove the last digit from original integer
integer_to_reverse //= 10
# Restore the negative sign if the original was negative
if is_negative:
reversed_integer = -reversed_integer
# Check for 32-bit integer overflow
if reversed_integer < -2**31 or reversed_integer > 2**31 - 1:
return 0
return reversed_integer
The best way to reverse an integer is to build the reversed number one digit at a time. We carefully extract digits from the original number and add them to the reversed number, while watching out for potential overflow issues before they happen.
Here's how the algorithm would work step-by-step:
def reverse_integer(number):
reversed_number = 0
is_negative = number < 0
if is_negative:
number = -number
while number > 0:
# Extract the last digit to append to reversed number.
last_digit = number % 10
# Check for potential overflow before adding the digit.
if reversed_number > 214748364 or \
(reversed_number == 214748364 and last_digit > 7):
return 0
if reversed_number < -214748364 or \
(reversed_number == -214748364 and last_digit < -8):
return 0
reversed_number = (reversed_number * 10) + last_digit
# Integer division to remove the last digit.
number //= 10
if is_negative:
reversed_number = -reversed_number
return reversed_number
Case | How to Handle |
---|---|
Positive integer with leading zeros | The reversal process will naturally remove the leading zeros. |
Negative integer with leading zeros | The sign should be preserved and the reversal will handle removing leading zeros. |
Integer is zero | Reversing zero should return zero. |
Maximum positive 32-bit integer (2147483647) | The reversal must check for overflow before returning the result. |
Minimum negative 32-bit integer (-2147483648) | The reversal must check for underflow (becoming smaller than the minimum) before returning the result. |
Reversed integer overflows positive range | Return 0 if the reversed integer is greater than 2147483647. |
Reversed integer overflows negative range | Return 0 if the reversed integer is less than -2147483648. |
Integer ends with zero after reversing | The solution does not need to handle trailing zeros because they'll be naturally added after a full reversal. |