Description
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³¹, 2³¹ - 1], then return 0.
Examples
Input:
x = 123Output:
321Explanation:
Reading digits from right to left: 3, 2, 1. Building the result: 3*100 + 2*10 + 1 = 321.
Input:
x = -123Output:
-321Explanation:
Reverse the absolute value: |−123| = 123 → 321. Restore the sign: −321.
Input:
x = 120Output:
21Explanation:
Reversing 120: digits are 0, 2, 1 from right to left. Leading zeros are dropped, so 021 becomes 21.
Input:
x = 0Output:
0Explanation:
Zero has no significant digits to reverse. The result remains 0.
Constraints
- •
-2³¹ ≤ x ≤ 2³¹ - 1