easyBasic Math Concepts

Reverse a Number

Reverse the digits of a signed 32-bit integer, preserving its sign. Return 0 if the reversed result overflows the signed 32-bit range. Do not use a wider integer type.

Work through the cases

3 examples

Input notation

An integer from -2147483648 through 2147483647.

Expected result

The reversed integer, or 0 on overflow.
Case 1

Input

1200

Expected output

21
Case 2

Input

1534236469

Expected output

0
Case 3

Input

-120

Expected output

-21

These notes use simplified input/output notation. The linked platform defines the method signature and judge format.

FROM IDEA TO IMPLEMENTATION

Understand the approach.

Compare the documented approaches below. Brute force, better, and optimal versions appear where available; a direct exercise may need only one approach.

Reversing via a string is the obvious idea, but the environment is assumed not to hold 64-bit integers. We cannot compute in a wider type and check overflow afterwards. Peeling the last digit and appending it is ans ≤ ftarrow 10 × ans+y. Before multiplying by 10, we must know whether the next value would leave [-2^{31}, 2^{31}-1]; if it would, return 0. That check collapses to whether ans still lies in [\lfloor mi/10 \rfloor, \lfloor mx/10 \rfloor]. Inside the range, one more digit is safe; outside, it has already overflowed.

Let's denote mi and mx as -2^{31} and 2^{31} - 1 respectively, then the reverse result of x, ans, needs to satisfy mi ≤ ans ≤ mx.

We can continuously take the remainder of x to get the last digit y of x, and add y to the end of ans. Before adding y, we need to check if ans overflows. That is, check whether ans × 10 + y is within the range [mi, mx].

If x \gt 0, it needs to satisfy ans × 10 + y ≤ mx, that is, ans × 10 + y ≤ ≤ ft \lfloor \frac{mx}{10} \right \rfloor × 10 + 7. Rearranging gives (ans - ≤ ft \lfloor \frac{mx}{10} \right \rfloor) × 10 ≤ 7 - y.

Next, we discuss the conditions for the inequality to hold:

- When ans \lt ≤ ft \lfloor \frac{mx}{10} \right \rfloor, the inequality obviously holds; - When ans = ≤ ft \lfloor \frac{mx}{10} \right \rfloor, the necessary and sufficient condition for the inequality to hold is y ≤ 7. If ans = ≤ ft \lfloor \frac{mx}{10} \right \rfloor and we can still add numbers, it means that the number is at the highest digit, that is, y must not exceed 2, therefore, the inequality must hold; - When ans \gt ≤ ft \lfloor \frac{mx}{10} \right \rfloor, the inequality obviously does not hold.

In summary, when x \gt 0, the necessary and sufficient condition for the inequality to hold is ans ≤ ≤ ft \lfloor \frac{mx}{10} \right \rfloor.

Similarly, when x \lt 0, the necessary and sufficient condition for the inequality to hold is ans ≥ ≤ ft \lfloor \frac{mi}{10} \right \rfloor.

Therefore, we can check whether ans overflows by checking whether ans is within the range [ ≤ ft \lfloor \frac{mi}{10} \right \rfloor, ≤ ft \lfloor \frac{mx}{10} \right \rfloor]. If it overflows, return 0. Otherwise, add y to the end of ans, and then remove the last digit of x, that is, x ≥ ts ≤ ft \lfloor \frac{x}{10} \right \rfloor.

The time complexity is O(log |x|), where |x| is the absolute value of x. The space complexity is O(1).

class Solution {
    public int reverse(int x) {
        int ans = 0;
        for (; x != 0; x /= 10) {
            if (ans < Integer.MIN_VALUE / 10 || ans > Integer.MAX_VALUE / 10) {
                return 0;
            }
            ans = ans * 10 + x % 10;
        }
        return ans;
    }
}

Reference code. Platform solutions may use supplied node types, imports, or collection libraries; foundational examples are complete programs.

Solutions adapted from Doocs / LeetCode contributors, licensed under CC BY-SA 4.0. Formatting changed; TypeScript versions converted to JavaScript. Supplemental implementations and approach notes added. Adapted solution material is available under the same license.