We’re preparing your current view and syncing the latest data.
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.
Assume the environment does not allow you to store 64-bit integers (signed or unsigned).
Example: Input: x = 123 Output: 321
Input: x = -123 Output: -321
Input: x = 120 Output: 21
Input: x = 0 Output: 0
An integer x within the 32-bit signed integer range.
An integer representing the reversed digits or 0 if overflow occurs.
−2^31 <= x <= 2^31 − 1
Example 1
Input
123
Output
321
Explanation
Reversing digits results in 321 which is within 32-bit range.
Example 2
Input
-123
Output
-321
Explanation
Reversing digits results in -321 which is within 32-bit range.
Example 3
Input
120
Output
21
Explanation
Leading zeros dropped after reversing digits.
Example 4
Input
0
Output
0
Explanation
Zero remains zero after reversing.