Likou Brush Questions: 8. Convert String to Integer (atoi)

Subject requirements

Insert picture description here
Original title link

Code

#define MaxValue pow(2,31)
class Solution {
    
    
public:
    int myAtoi(string s) {
    
    
        bool hasNumber = false;
        char sigh = '+';
        int number;
        for (unsigned i = 0; i < s.size(); i++)
        {
    
    
            //跳过开头空格
            if (s[i] == ' ')
            {
    
    
                continue;
            }
            else 
            {
    
    
            //匹配符号
            if (s[i] == '+' || s[i] == '-')
            {
    
    
                sigh = s[i++];
            }
            //匹配数字
            if (charIsNumber(s[i]))
            {
    
    
                hasNumber = true;
                number = numberFun(s, i);
            }
            break;
            }
        }
        //有效数字判断
        if (hasNumber)
        {
    
    
            if (sigh == '+')
            {
    
     
                if (number == -MaxValue)
                {
    
    
                    return MaxValue - 1;
                }
                return -number;
            }
            else
            {
    
    
                if (sigh == '-')
                {
    
    
                    return number;
                }
            }
        }
        return 0;
    }
    bool charIsNumber(char c)
    {
    
    
        if (c >= '0' && c <= '9')
            return true;
        else
            return false;
    }
    int numberFun(string& s, unsigned index)
    {
    
    
        int number = 0;
        while (charIsNumber(s[index]) && index < s.size())
        {
    
    
            //2147483648是2的31次方,需要判断当前数字是否超出界限
            if (number < - static_cast<int>( MaxValue / 10))
            {
    
    
                return -MaxValue;
            }
            else if (number == static_cast<int>(-MaxValue / 10))
            {
    
    
                //往前看一位:如果下一位还是数字,则肯定超了
                if (charIsNumber(s[index+1]))
                {
    
    
                    return -MaxValue;
                }
                else
                {
    
    
                    if (s[index] > '8')
                    {
    
    
                        return -MaxValue;
                    }
                    else
                    {
    
    
                        return number*10 - (s[index] - '0');
                    }
                }
            }
            number = number * 10 - (s[index] - '0');
            ++index;
        }   
        return number;
    }
};

prepare in advance

Wrote two sub-functions
bool charIsNumber function to determine whether a character is a number.
The int numberFun function enters a string and the starting position of the first number. This function will automatically output a continuous string of numbers starting with this position in the string. The output result is negative

the whole idea

1. First read and discard the space character at the beginning of the string.
2. Read the first non-blank character.
If the first character is + or -, record the symbol. Then call the numberFun function to read the continuous number string after the symbol.
If the first character is a number, the numberFun function is called directly to read the continuous number string beginning with this character.
If the first character is other, it returns 0.

3. Sub-function numberFun: Read the number string.
Here we choose negative number as the return value, because the range that negative number can represent is 1 more than positive number. If you choose positive number as return value, it cannot represent -2 to the 31st power. digital

First of all, you need to know that the result of -2 to the 31st power is 2147483648, which is used as the criterion for judging the negative boundary value

  • If the value of this round is less than -214748364 , the value multiplied by 10 will definitely exceed the limit. So directly return to the 31st power of -2
  • If the value of this round is equal to -214748364 , then look forward to one more character:
    • If the next character is still a number , it must be out of bounds and return directly to the 31st power of -2
    • If the next character is not a number , then continue to judge:
      • If the character number x<=8 read this time , output -214748364x
      • If the character number x=9 read this time exceeds the negative upper limit, output -2 to the 31st power
  • If the value of this round is greater than -214748364, it is normal. num = num*10-x; (x is the number represented by this round of characters)

4. Synthesize the symbol and the number to get the result:

  • If the sign is positive

    • When number<=-2 to the 31st power, return 2 to the 31st power-1
    • Return -number in other cases
  • If the sign is negative

    • Return number

What learned

1. A deeper understanding of the meaning of 4 bytes in int. 4 bytes are 32 bits, that is, the expression range of int is:
Insert picture description here

2. It should be more detailed when dealing with the boundary value (the boundary processing of this question took a morning)

Guess you like

Origin blog.csdn.net/youyadefeng1/article/details/113405483