Skip to content

Reverse Integer

MediumBit Manipulation & Math
Decode it first. Clues, constraints, pattern and plan before any code.

Given a signed 32-bit integer x, return it with its decimal digits reversed, keeping the sign. If the reversed value falls outside the signed 32-bit range [-2^31, 2^31 - 1], return 0.

x = 123         →  321
x = -123        →  -321
x = 120         →  21          (the trailing zero becomes a leading zero and disappears)
x = 1534236469  →  0           (9646324351 does not fit)

Constraints

  • -2^31 <= x <= 2^31 - 1
  • Assume you cannot store a 64-bit integer: detect overflow before it happens.