This is the 11th day of my participation in the Gwen Challenge in November. Check out the details: The last Gwen Challenge in 2021

The title

You are given a 32-bit signed integer x that returns the result of reversing the numeric portion of x.

If the inverted integer exceeds the range of 32-bit signed integers [−231, 231 − 1], 0 is returned.

Assume that the environment does not allow storage of 64-bit integers (signed or unsigned).

Example 1:

Input: x = 123 Output: 321

Example 2:

Input: x = -123 Output: -321

Example 3:

Input: x = 120 Output: 21

Example 4:

Input: x = 0 Output: 0

Train of thought

When you see this problem, the first thing that comes to mind is to convert to a string and then flip it, so write the first version quickly

public int reverse(int x) {
    StringBuffer s = new StringBuffer(String.valueOf(x));
    if(x> 0) {return Integer.valueOf(s.reverse().toString());
    }else if(x <0) {return - Integer.valueOf(s.reverse().toString().substring(0,s.length()-1));
    }else{
        return 0; }}Copy the code

Execution error message:

java.lang.NumberFormatException: For input string: "9646324351"at line 68, java.base/java.lang.NumberFormatException.forInputString at line 658, java.base/java.lang.Integer.parseInt at line 989, java.base/java.lang.Integer.valueOf at line 6, Reverse at line 54, __DriverSolution__.__helper__ at line 84, __Driver__. MainCopy the code

This error occurs because when the input is 1534236469, it is flipped to 9646324351

But then integer.valurof () would exceed the maximum value of int. 2147483647

If you can see what the problem is, you just return 0 when you go out of range, as they say

We can use exceptions to control the return, so when there’s an exception, we just return 0

public static int reverse(int x) {
    try {
        StringBuffer s = new StringBuffer(String.valueOf(x));
        if (x > 0) {
            return Integer.valueOf(s.reverse().toString());
        } else if (x < 0) {
            return -Integer.valueOf(s.reverse().toString().substring(0, s.length() - 1));
        } else {
            return 0; }}catch (Exception e) {
        return 0; }}Copy the code

Execution time: 2 ms, beating 22.09% of all Java commits

Memory consumption: 35.7 MB, beating 22.48% of all Java commits

The result is not very efficient. Consider other methods. String operations are relatively time-consuming

Method 2

Because string operations are time-consuming, mathematical formulas are used to implement them

Take the last digit of the current number in turn and add.

The key is how to determine integer overflow:

The number after each operation can be stored in a temporary variable, which can be reversed,

If the result is different from that before the operation, an overflow occurs and 0 is returned

public int reverse(int x) {
    int result = 0;
    while(x ! =0) {// The current operation bit
        int cuur = x%10;
        //result*10 = 123, 3*10, 3*10*10+2*10, 3*10*10+2*10*10 +1*10
        int temp =  cuur + result*10;
        // The reverse of the previous line to determine whether the int range is exceeded
        if((temp - cuur)/10! = result){return 0;
        }
        x = x/10;
        result = temp;
    }
    return result;
}
Copy the code

Execution time: 1 ms, beating 97.47% of users in all Java commits

Memory consumption: 35.6 MB, beating 30.81% of all Java commits

Method three

Int numbers cannot be flipped beyond the scope of long, so you can use long and int after conversion

Check for equality to see if it is outside the int range

public int reverse(int x) {
    long result = 0;
    while(x ! =0) {int cuur = x%10;
        result = result *10 + cuur;
        x = x/10;
    }
    // If the range is not exceeded, the value does not change after the strong type
    int res = (int)result;
    return res == result? res: 0;
}
Copy the code

Execution time: 1 ms, beating 97.47% of users in all Java commits

Memory consumption: 35.2 MB, beating 94.37% of all Java commits

Learn more today and say less tomorrow. Come on!