Reverse Integer

EasyBit Manipulation

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 = 123
Output:321
Explanation:

Reading digits from right to left: 3, 2, 1. Building the result: 3*100 + 2*10 + 1 = 321.

Input:x = -123
Output:-321
Explanation:

Reverse the absolute value: |−123| = 123 → 321. Restore the sign: −321.

Input:x = 120
Output:21
Explanation:

Reversing 120: digits are 0, 2, 1 from right to left. Leading zeros are dropped, so 021 becomes 21.

Input:x = 0
Output:0
Explanation:

Zero has no significant digits to reverse. The result remains 0.

Constraints

  • -2³¹ ≤ x ≤ 2³¹ - 1

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!