You are given a signed 32-bit integer x.
Return x after reversing each of its digits. After reversing, if x goes outside the signed 32-bit integer range [-2^31, 2^31 - 1], then return 0 instead.
Solve the problem without using integers that are outside the signed 32-bit integer range.
Example 1:
Input: x = 1234
Output: 4321Example 2:
Input: x = -1234
Output: -4321Example 3:
Input: x = 1234236467
Output: 0Constraints:
-2^31 <= x <= 2^31 - 1
You should aim for a solution with O(1) time and O(1) space.
A straightforward approach would be to convert the given integer to a string, reverse it, convert it back to an integer using a long type, and return 0 if the result exceeds the integer range. Can you think of a better way?
We initially declare the result res as an int with a value of 0. We iterate through the given integer, extracting digits one by one. Before appending a digit to res, we consider multiple cases. Can you determine them? Maybe you should think about overflow.
Let MAX be the maximum positive integer and MIN be the minimum negative integer. We iterate through each digit and check for overflow before updating res. If res > MAX / 10 or res < MIN / 10, return 0. If res == MAX / 10 and the current digit is greater than MAX % 10, return 0. If res == MIN / 10 and the current digit is less than MIN % 10, return 0. Otherwise, append the digit to res and continue.
Before attempting this problem, you should be comfortable with:
We want to reverse the digits of an integer x (for example, 123 -> 321, -120 -> -21).
A very simple way to do this is:
x was negativeFinally, the problem usually requires that the answer must fit in a 32-bit signed integer range:
-2^31 to 2^31 - 10.This approach is beginner-friendly because it uses direct operations on strings instead of manual digit math.
x in org so we remember its sign.x to its absolute value.res (this automatically removes leading zeros).res negative.res fits in the 32-bit signed integer range:0.We want to reverse the digits of an integer while preserving its sign and ensuring the result fits within the 32-bit signed integer range.
Instead of reversing digits using strings, this approach uses pure arithmetic and recursion.
The idea is simple:
0Recursion naturally fits this process because each step reduces the problem size by one digit.
+ or -) and work with its absolute value.rec that takes:nrevn is 0, return rev.n % 10)rev (rev * 10 + digit)n // 10)0.class Solution:
def reverse(self, x: int) -> int:
def rec(n: int, rev: int) -> int:
if n == 0:
return rev
rev = rev * 10 + n % 10
return rec(n // 10, rev)
sign = -1 if x < 0 else 1
x = abs(x)
reversed_num = rec(x, 0)
reversed_num *= sign
if reversed_num < -(1 << 31) or reversed_num > (1 << 31) - 1:
return 0
return reversed_numWe want to reverse the digits of an integer without using strings, while also ensuring the result fits within the 32-bit signed integer range.
The key idea is to build the reversed number digit by digit:
resHowever, before appending a new digit, we must check for overflow.
If multiplying res by 10 (and adding the new digit) would exceed the 32-bit signed integer limits, we immediately return 0.
This approach closely matches how integer reversal works at a low level and is both efficient and safe.
MIN = -2^31MAX = 2^31 - 1res = 0 to store the reversed number.x is not 0:digit of x.x.res, check:res * 10 + digit would overflow the 32-bit signed integer range:0.res = res * 10 + digit.res.class Solution:
def reverse(self, x: int) -> int:
MIN = -2147483648 # -2^31,
MAX = 2147483647 # 2^31 - 1
res = 0
while x:
digit = int(math.fmod(x, 10))
x = int(x / 10)
if res > MAX // 10 or (res == MAX // 10 and digit > MAX % 10):
return 0
if res < MIN // 10 or (res == MIN // 10 and digit < MIN % 10):
return 0
res = (res * 10) + digit
return resA critical mistake is multiplying res by 10 and adding the digit before checking if the result overflows. By that point, overflow has already occurred and the value is corrupted. The overflow check must happen before the multiplication to determine if the next operation would exceed the 32-bit signed integer bounds.
Different programming languages handle the modulo operation differently for negative numbers. In Python, -123 % 10 returns 7, not -3. Using the wrong modulo behavior leads to incorrect digit extraction for negative inputs. Use language-specific functions like math.fmod() in Python or ensure consistent handling across languages.
The 32-bit signed integer range is asymmetric: -2^31 to 2^31 - 1. This means the minimum value has a larger absolute value than the maximum. When checking for overflow, both boundaries must be validated separately. Simply checking against one limit and assuming symmetry causes edge cases like reversing 1534236469 to fail silently.