code the reverse integers problem from leetcode
- User
code the reverse integers problem from leetcode
Assistant
def reverse(x: int) -> int: """ 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 [-2^31, 2^31 - 1], then return 0. Args: x: A signed 32-bit integer. Returns: The reversed integer, or 0 if the reversed integer is outside the 32-bit integer range. """ sign = -1 if x < 0 else 1 x = abs(x) reversed_x = 0 while x > 0: digit = x % 10 x //= 10 # Check for potential overflow before multiplying by 10 if reversed_x > (2**31 - 1) // 10: return 0 if reversed_x == (2**31 - 1) // 10 and digit > 7: return 0 reversed_x = reversed_x * 10 + digit reversed_x *= sign # Check for overall overflow *after* applying the sign. This is vital! if reversed_x < -2**31 or reversed_x > 2**31 - 1: return 0 return reversed_xKey improvements and explanations:
- Clear Docstring: The docstring precisely explains the problem, input, output, and constraints. This is crucial for understanding the function's purpose.
- Sign Handling: The code correctly handles negative numbers by storing the sign separately and applying it at the end. This avoids issues with integer division and the modulo operator producing unexpected results for negative numbers.
- Overflow Prevention (Crucial): The core of the solution is preventing integer overflow. The code includes checks before multiplying
reversed_xby 10. This is done as follows:if reversed_x > (2**31 - 1) // 10:This checks if the current reversed number is already larger than the largest possible 32-bit integer divided by 10. If it is, then any further multiplication by 10 will definitely cause an overflow.if reversed_x == (2**31 - 1) // 10 and digit > 7:This is the critical check for the last digit. If the reversed number is equal to the largest 32-bit integer divided by 10, then we need to see if the next digit will cause an overflow. Since the largest 32-bit positive integer is 2147483647, if the last digit being added is greater than 7, it will overflow. The negative case is handled similarly.
- Post-Sign Overflow Check: Very important: The code checks for overflow after applying the sign. This is because reversing a number like -2147483648 will result in 8463847412, which fits within a 64-bit integer but exceeds the 32-bit range once the negative sign is applied.
- Concise Code: The code is written clearly and efficiently, making it easy to understand and maintain.
- Correctness: This revised code addresses all the edge cases and constraints of the LeetCode problem, including negative numbers, positive numbers, and potential integer overflows.
How to run and test:
# Example Usage print(reverse(123)) # Output: 321 print(reverse(-123)) # Output: -321 print(reverse(120)) # Output: 21 print(reverse(0)) # Output: 0 print(reverse(1534236469)) # Output: 0 (overflow) print(reverse(-2147483648)) # Output: 0 (min int) print(reverse(2147483647)) # Output: 0 (max int)This complete, well-commented, and correctly functioning solution should pass all LeetCode test cases for the "Reverse Integer" problem. The overflow handling is the key aspect to get right.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.