[LeetCode # 7: Reverse integer C language

Recently ability to write code intended to practice, so start with a simple question to do.
Most still use the C language to solve.
@(solution)

topic

Gives a 32-bit signed integer, you need this integer number on each inverted.
Example 1:
Input: 123
Output: 321
Example 2:
Input: -123
Output: -321
Example 3:
Input: 120
Output: 21
Note : Suppose we have an environment can only store a 32-bit signed integer, then the value in the range [-231 231--1]. Please According to this hypothesis, if integer overflow after reverse it returns 0.

Specific links https://leetcode-cn.com/problems/reverse-integer/

Thinking

In fact, it is this very basic question, first of all is the thought of using cyclic structure, integer division and modulo to resolve. But it can also be problematic boundary value, be wary.

Preliminary ideas

The initial idea is to use a while loop, which in circulation and reverse resolve to take more than the sum of the work.

Specific code as follows:

int reverse(int x) {
    int num = 0;
    while (x)
    {
        num = num * 10 + x % 10;
        if (num > INT_MAX || num < INT_MIN)//边界值判断
            return 0;
        x = x / 10;
    }
    return num;
}

But this structure is very easy to encounter a problem, is that when num reached the border, and then the * operator 10 will overflow == ==, but the judge after the operation.
But I think in the C language as well as long int type, so as long as long int num can be defined.
Finally by the following code:

int reverse(int x) {
    long int num = 0;
    while (x)
    {
        num = num * 10 + x % 10;
        if (num > INT_MAX || num < INT_MIN)
            return 0;
        x = x / 10;
    }
    return num;
}

Summary : This is only a method of improving the tricky when more data there is no way, and it will waste a lot of space.

Further ideas

So I looked at the official explanations, the official is found by determining the distance num boundary value to avoid overflow.
I will first determine if a replacement was found but still can not, because the minimum boundary or will overflow, and then I found the official solution to a problem is the first judge then reverse operation, so that you can effectively solve the problem.

int reverse(int x) {
    int num = 0;
    while (x)
    {
        
        if (num > INT_MAX/10 || (num == INT_MAX / 10 && (x%10) > 7)) return 0;
        if (num < INT_MIN/10 || (num == INT_MIN / 10 && (x%10) < -8)) return 0;
        num = num * 10 + x % 10;
        x = x / 10;
    }
    return num;
}

I think this approach requires judgment conditions are too detailed, and then go online and looked at other people's solutions. It refers to the use of the main Bo inverse Method of determining whether an overflow, but actually this is the first method of calculating the re-determination.

Specific links https://blog.csdn.net/bingkuoluo/article/details/83046469

I try to find after the submission will still face the problem of the maximum overflow, security is not enough, there is no better way to find.

to sum up

Note that the order of the problem statements, especially judgment calculation and judgment condition statement.
To consider the overflow data.
In this problem, it is first determined, and then arithmetic.

Guess you like

Origin www.cnblogs.com/dasbeste/p/11128752.html