Leetcode problem solution-8. String conversion integer (atoi)

Topic link

Title description:

Please come to implement a atoifunction, it can convert a string to an integer.

First, the function discards useless beginning space characters as needed, until it finds the first non-space character. The following conversion rules are as follows:

If the first non-blank character is a positive or negative sign, combine this sign with as many consecutive digital characters as possible to form a signed integer.
If the first non-blank character is a number, it is directly combined with the following consecutive number characters to form an integer.
The string may also have extra characters after the valid integer part, so these characters can be ignored and they should not affect the function.
Note: If the first non-space character in the string is not a valid integer character, the string is empty or the string contains only blank characters, your function does not need to be converted, that is, it cannot be converted effectively.

In any case, if the function cannot perform a valid conversion, please return 0.

prompt:

White space characters in this question only include space characters ' '.
Assuming that our environment can only store 32-bit signed integers, the value range is [−2^31, 2^31 − 1]. If the value exceeds this range, please return INT_MAX (2^31 − 1) or INT_MIN (−2^31).

Example 1:

Input: "42"
Output: 42

Example 2:

Input: "-42"
Output: -42
Explanation: The first non-blank character is'- ', which is a negative sign.
We try our best to combine the minus sign with all subsequent numbers, and finally get -42.

Example 3:

Input: "4193 with words"
Output: 4193
Explanation: The conversion ends at the number '3' because the next character is not a number.

Example 4:

Input: "words and 987"
Output: 0
Explanation: The first non-blank character is'w', but it is not a number or positive or negative sign.
Therefore, effective conversion cannot be performed.

Example 5:

Input: "-91283472332"
Output: -2147483648
Explanation: The number "-91283472332" exceeds the range of a 32-bit signed integer.
Therefore INT_MIN (−2^31) is returned.


Careful simulation

  There are many situations considered in this question, and the only requirement is patience. The process is as follows:

  1. Remove the spaces at the beginning of the string until the first non-blank character is encountered;
  2. Judge whether the first non-empty character is a number or a sign, if not, return 0, and if yes, continue to judge whether it is a sign;
  3. Set an flagidentifier as a plus or minus sign, and that's it at the end result * flag;
  4. The next step is to continue to judge until it encounters a space or sign or letter (except for numbers);
  5. result = result * 10 + numConvert a string to a number in a loop;
  6. Don’t forget about the out-of- bounds situation, you INT_MAXcan judge whether it is greater than each time after accumulation , flagthen it comes in handy to determine whether it is INT_MAXor is INT_MIN;

Code

class Solution {
    
    
public:
    int myAtoi(string str) {
    
    
        int indexNum = 0;
        int len = str.size();
        while(indexNum < len && str[indexNum] == ' ')
            indexNum++;
        
        if(indexNum == len || (str[indexNum] > 'A' && str[indexNum] <= 'z'))
            return 0;
        
        long long result = 0;
        int flag = 1;

        if(str[indexNum] == '-'){
    
    
            flag = -1;
            indexNum++;
        }
        else if(str[indexNum] == '+')
            indexNum++;
        
        while(indexNum < len && str[indexNum] >= '0' && str[indexNum] <= '9'){
    
    
            int num = str[indexNum] - '0';
            result = result * 10 + num;
            indexNum++;
            if(result > INT_MAX){
    
    
                return (flag == -1) ?  INT_MIN : INT_MAX;
            }
        }

        return result * flag;
    }
};


If there are mistakes or not rigorous, please correct me, thank you very much.
My blog: http://breadhunter.gitee.io

Guess you like

Origin blog.csdn.net/weixin_40807714/article/details/105289559