Reverse Integer
Part of Practice: Arithmetic Without +
Given a signed 32-bit integer x, return x with its decimal digits reversed. A negative sign stays in front, and trailing zeros disappear when reversed (120 becomes 21).
The twist: if the reversed value falls outside the signed 32-bit range, from -2^31 to 2^31 - 1, return 0 instead. You must assume your environment cannot hold numbers outside that range even temporarily — so the overflow has to be detected before the multiplication that would cause it, not after.
Examples
- Input:
- x = 123
- Output:
- 321
- Input:
- x = -123
- Output:
- -321
The digits reverse; the sign stays attached to the front.
- Input:
- x = 1534236469
- Output:
- 0
Reversed, the digits read 9646324351, which is larger than 2^31 - 1 = 2147483647, so the answer is 0.
Hints
One at a time — struggle first, peek second.
Your thinking space — verify on LeetCode, then mark it.