LeetCode Brushing Notes 07 Reverse Integer

Author: Ma Zhifeng
link: https: //zhuanlan.zhihu.com/p/23967316
Source: know almost
copyrighted by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.

link

leetcode.com/problems/z

topic

Example 1: x = 123, return 321
Example 2: x = -123, return -321

The rules seem very simple, but have you thought of some special scenarios?

The person who asked the question is also very mindful, so I folded the reminder and reminded us to think more.

Yes, think about the special scenes and then look down

Well, if you have really thought about it, here are the tips for the title:

  1. If the last few bits are 0, what should our output look like? Like 10100?
  2. Are you aware that the inverted integer may overflow? If it is a 32-bit integer, its maximum value is 2147483648, then 1000000003 will overflow after inversion. For this case, we require the output to be 0

Function definition

The function definition given by leetcode looks like this:

class Solution {  
public:  
    int reverse(int x) {  

    }  
};

Problem solving ideas

If overflow is not considered, the idea of ​​this topic is easy to think of:

  1. To get the number on each digit, you can use integer division and remainder operations. You have to write a loop, dividing by 10 for the first time, and dividing by 100 for the second time. . .
  2. For the resulting sequence of numbers, multiply the first digit by 1, the second digit by 10, and the third digit by 100. . . , And then add up to get the reversal number

For overflow, the simple and rude way is to see if try...catch... is suitable

  • If appropriate, return 0 directly in the catch.
  • If it is not suitable, we have to find the maximum value of the integer type

Pseudocode

x = 123;
x is the remainder of 10 to get 3, x is divided by 10 to get 12,
12 is to get the remainder of 10, and x is divided by 10 to get 1.
1 is to get the remainder of 10 to get 1, and x is divided by 10 to get 0

1*1 + 2*10 + 3*100 = 321

Code

class Solution {  
public:  
    int reverse(int x) {  
        if (x < 10 && x > -10)  
        {  
            return x;  
        }  

        vector<int> vecDigits;  
        int iTemp = x;  
        do  
        {  
            vecDigits.push_back(iTemp % 10);  
            iTemp = iTemp / 10;  
        } while (iTemp != 0);  

        auto iSize = vecDigits.size();  
        int iFactor = 1;  
        int iResult = 0;  
        for (auto i = iSize - 1; i >= 0; --i)  
        {  
            iResult += iFactor * vecDigits[i];  
            iFactor *= 10;  
        }  

        return iResult;  
    }  
};

After the code is written, substitute x=123 into it to check

Well, everything looks fine.

But when I submit it, "Runtime Error" appears! Why? !

If you don’t understand, you can go back and look at page 34 of "c++ primer"

Yes, there is an endless loop. Sadly. When I was reading, I felt that I would not make such a mistake. . .

class Solution {  
public:   
    int reverse(int x) {  
        if (x < 10 && x > -10)  
        {  
            return x;  
        }  

        vector<int> vecDigits;  
        int iTemp = x;  
        do  
        {  
            vecDigits.push_back(iTemp % 10);  
            iTemp = iTemp / 10;  
        } while (iTemp != 0);  

        auto i = vecDigits.size();  
        int iFactor = 1;  
        int iResult = 0;  

        while (i > 0)  
        {  
            --i;  
            iResult += iFactor * vecDigits[i];  
            iFactor *= 10;  
        }  

        return iResult;  
    }  
};

After submitting the code to see, the overflow error is reported because we have not dealt with this situation

After we changed the input to a very large number, we found that there was no exception during runtime. Or use the boundary value to judge honestly

I didn’t think of a good solution for this realization, so I looked at the discussion area

discuss.leetcode.com/to

discuss.leetcode.com/to

We refer to one of the modified codes

class Solution {  
public:  
    int reverse(int x) {  
        if (x < 10 && x > -10)  
        {  
            return x;  
        }  

        vector<int> vecDigits;  
        int iTemp = x;  
        do  
        {  
            vecDigits.push_back(iTemp % 10);  
            iTemp = iTemp / 10;  
        } while (iTemp != 0);  

        auto i = vecDigits.size();  
        long long iFactor = 1;  
        long long iResult = 0;  

        while (i > 0)  
        {  
            --i;  
            iResult += iFactor * vecDigits[i];  
            iFactor *= 10;  
        }  

        if (iResult > INT_MAX || iResult < INT_MIN)  
        {  
            return 0;  
        }  

        return iResult;  
    }  
};

Well, finally Accepted.

But looking at the discussion area, we can find that the two loops can be merged, this is left for everyone

In addition, you can also try to convert an integer to a string~

Guess you like

Origin blog.csdn.net/qq_26751117/article/details/53442415