Skip to main content

Reverse Integer

MediumPremium

Given a signed 32-bit integer x, return x with its digits in reverse order. If the reversed number falls outside the 32-bit range [-2^31, 2^31 - 1], return 0 instead.

Assume you can't store any value outside that range, even temporarily, so you can't build the reversed number in a 64-bit integer and check it afterward.

Example 1

Input: x = -123 Output: -321

The digits reverse and the sign stays in front.

Example 2

Input: x = 1534236469 Output: 0

Reversed, this is 9646324351, which is larger than 2^31 - 1 = 2147483647.

Constraints

  • -2^31 <= x <= 2^31 - 1