Skip to content
BASA DSA

Reverse Integer

MediumBit Manipulation

Reverse the decimal digits of a signed integer, preserving its sign and discarding reversed leading zeros. Return zero if the result is outside the signed 32-bit range.

Example 1

Input
x = 120
Output
21

Explanation

Reversing 120 gives digits 021; discard the leading zero to obtain 21.

Example 2

Input
x = -340
Output
-43

Explanation

Reverse magnitude 340 to 43 and restore the negative sign.

Example 3

Input
x = 1534236469
Output
0

Explanation

9646324351 exceeds the signed 32-bit maximum, so return zero.

Example 4

Input
x = -2147483648
Output
0

Explanation

Reversed magnitude 8463847412 is outside the signed 32-bit range even with a negative sign.

Constraints

  • -2147483648 <= x <= 2147483647
  • Do not rely on a 64-bit integer type

Function contractreverseArguments: json
Loading draft…
SolutionRuns on your device
Loading draft…