Practice/Oracle/Leetcode 7. Reverse Integer
CodingMust
Given a 32-bit signed integer, reverse its digits while preserving the sign. If reversing the digits causes the result to exceed the range of a 32-bit signed integer (from -2³¹ to 2³¹ - 1), return 0 instead.
Your solution must detect potential overflow conditions without relying on 64-bit integer types or larger data structures.
Example 1:
Input: x = 123 Output: 321 Explanation: Simply reverse the digits of the positive number.
Example 2:
Input: x = -456 Output: -654 Explanation: Reverse the digits while keeping the negative sign.
Example 3:
Input: x = 120 Output: 21 Explanation: After reversal, leading zeros are dropped.
Example 4:
Input: x = 1534236469 Output: 0 Explanation: The reversed number 9646324351 exceeds 2,147,483,647 (the maximum 32-bit signed integer), so we return 0.