7. Reverse Integer (Easy)
Pyodide¶
# Type here
Run¶
class Solution:
def reverse(self, x: int) -> int:
INT_MAX = 2147483647
INT_MIN = -2147483648
reversed_x = 0
while x != 0:
digit = x % 10
if x < 0 and digit > 0:
digit -= 10
x = (x - digit) // 10
if reversed_x > INT_MAX // 10 or (reversed_x == INT_MAX // 10 and digit > 7):
return 0
if reversed_x < INT_MIN // 10 or (reversed_x == INT_MIN // 10 and digit < -8):
return 0
reversed_x = reversed_x * 10 + digit
return reversed_x
sol = Solution()
print("--- Reverse Integer Examples ---")
# Test Case 1: Positive number
x1 = 123
result1 = sol.reverse(x1)
print(f"Input: {x1}")
print(f"Output: {result1} (Expected: 321)")
# Test Case 2: Negative number
x2 = -123
result2 = sol.reverse(x2)
print(f"Input: {x2}")
print(f"Output: {result2} (Expected: -321)")
# Test Case 3: Number with trailing zeros
x3 = 120
result3 = sol.reverse(x3)
print(f"Input: {x3}")
print(f"Output: {result3} (Expected: 21)")
# Test Case 4: Positive Overflow Check (Max is 2147483647)
# Reversing 1534236469 gives 9646324351, which exceeds INT_MAX
x4 = 1534236469
result4 = sol.reverse(x4)
print(f"Input: {x4}")
print(f"Output: {result4} (Expected: 0 - Overflow)")
# Test Case 5: Negative Overflow Check (Min is -2147483648)
# Reversing -1534236469 gives -9646324351, which is less than INT_MIN
x5 = -1534236469
result5 = sol.reverse(x5)
print(f"Input: {x5}")
print(f"Output: {result5} (Expected: 0 - Overflow)")
# Test Case 6: Exact INT_MAX reversal (No Overflow)
# Reversing 1463847412 (214748364 * 10 + 7) -> 2147483641
x6 = 1463847412 # Just a large number whose reverse is safe
result6 = sol.reverse(x6)
print(f"Input: {x6}")
print(f"Output: {result6} (Expected: 2147483641)")
Solution¶
class Solution:
def reverse(self, x: int) -> int:
INT_MAX = 2147483647
INT_MIN = -2147483648
reversed_x = 0
while x != 0:
digit = x % 10
if x < 0 and digit > 0:
digit -= 10
x = (x - digit) // 10
if reversed_x > INT_MAX // 10 or (reversed_x == INT_MAX // 10 and digit > 7):
return 0
if reversed_x < INT_MIN // 10 or (reversed_x == INT_MIN // 10 and digit < -8):
return 0
reversed_x = reversed_x * 10 + digit
return reversed_x
Function Description¶
Source code in python/_7.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | |
reverse(x)
¶
Source code in python/_7.py
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | |
Explanation¶
Given a signed 32-bit integer, \(x\), the task is to return its digits reversed. For example, if \(x = 123\), the output should be \(321\). If \(x = -123\), the output should be \(-321\). The number's sign must be preserved in the reversed result. The input integer \(x\) is within the range \([-(2^{31}), 2^{31} - 1]\), which defines the limits of a standard 32-bit signed integer.
The primary difficulty in this problem is handling integer overflow. The reversed integer might exceed the maximum value of a 32-bit signed integer (\(2^{31} - 1\)) or fall below its minimum value (\(-(2^{31})\)), even if the original number \(x\) was within range. For example, reversing \(1,534,236,469\) results in \(9,646,324,351\), which is larger than \(2,147,483,647\). Therefore, before appending the next digit, a check must be performed to ensure the current reversed number, multiplied by 10 and added to the new digit, does not cause an overflow.
The most robust approach involves iteratively extracting the last digit of \(x\) using the modulo operator (\(x \pmod{10}\)) and building the reversed number. In each iteration, we check for potential overflow before the multiplication step. Specifically, if reversed > INT_MAX / 10 (or reversed < INT_MIN / 10), we know the next multiplication will definitely overflow. If it's on the edge (e.g., reversed == INT_MAX / 10), we then check the last digit itself to see if it causes the final overflow. If an overflow is detected at any point, the function must immediately return \(0\).