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 - 1When 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:
Imagine you have a number written down on a slip of paper and you want to read it backwards from right to left. The most straightforward approach is to pull off the last digit, write it down first, and repeat this process until you have moved every single digit to the new backward position.
Here's how the algorithm would work step-by-step:
def reverse_integer(original_number):
# We preserve the sign beforehand because modulo and division behave differently on negative integers
is_negative = original_number < 0
remaining_digits = abs(original_number)
reversed_result = 0
while remaining_digits > 0:
last_digit = remaining_digits % 10
reversed_result = (reversed_result * 10) + last_digit
remaining_digits = remaining_digits // 10
# We apply the 32-bit signed integer boundaries to comply with standard platform constraints
if reversed_result > 2147483647:
return 0
# We restore the original sign back to the reversed digits to match the input polarity
if is_negative:
return -reversed_result
return reversed_resultThe optimal strategy for reversing a whole number is to repeatedly peel off the very last digit from the original number and attach it to the end of our new reversed number. We do this by working from right to left, moving digits over to make room as we build the reversed result piece by piece.
Here's how the algorithm would work step-by-step:
class Solution:
def reverse(self, original_integer: int) -> int:
reversed_number = 0
if original_integer < 0:
sign_multiplier = -1
remaining_digits = -original_integer
else:
sign_multiplier = 1
remaining_digits = original_integer
max_limit = 2**31 - 1
min_limit = -2**31
while remaining_digits != 0:
last_digit = remaining_digits % 10
remaining_digits //= 10
# Prevent integer overflow before applying the new digit to the accumulator.
if reversed_number > (max_limit - last_digit) // 10:
return 0
reversed_number = reversed_number * 10 + last_digit
final_result = reversed_number * sign_multiplier
# Ensure the final signed integer remains within standard 32-bit bounds.
if final_result < min_limit or final_result > max_limit:
return 0
return final_result| Case | How to Handle |
|---|---|
| Signed 32-bit integer overflow during reversal | We check if the accumulated result will exceed the integer limits before multiplying by ten and adding the next digit. |
| Negative signed integers | The modulo and division operators preserve the negative sign in languages like C++ and Java, naturally handling negative numbers. |
| Input integer is exactly zero | The algorithm enters the loop once, pops zero, and correctly returns zero without any issues. |
| Numbers ending in zero such as 120 | The trailing zero becomes a leading zero during math operations and is naturally dropped, resulting in 21. |
| Boundary values like INT_MAX or INT_MIN | Reversing these extreme values causes an immediate overflow, which triggers our overflow check and returns zero. |
| Single-digit integers | The loop runs once and returns the exact same digit since single-digit numbers are already reversed. |