Reverse Integer
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
x = 12021Explanation
Reversing 120 gives digits 021; discard the leading zero to obtain 21.
Example 2
x = -340-43Explanation
Reverse magnitude 340 to 43 and restore the negative sign.
Example 3
x = 15342364690Explanation
9646324351 exceeds the signed 32-bit maximum, so return zero.
Example 4
x = -21474836480Explanation
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 contract
reverseArguments: jsonSolutionRuns on your device
Loading draft…
Run: 3 examples; Submit: 33 cases, on your device.
120