leetcode -- string to integer (atoi)

Posted by agnaldovb on Wed, 09 Mar 2022 10:09:35 +0100

subject

Please implement a myAtoi(string s) function to convert the string into a 32-bit signed integer (similar to the atoi function in C/C + +).

The algorithm of the function myAtoi(string s) is as follows:

  • Read in the string and discard useless leading spaces
  • Check whether the next character (assuming it is not at the end of the character) is a positive or negative sign, and read the character (if any). Determine whether the final result is negative or positive. If neither exists, the result is assumed to be positive.
  • Read the next character until you reach the next non numeric character or the end of the input. The rest of the string will be ignored.
  • Convert the numbers read in the previous steps into integers (i.e., "123" - > 123, "0032" - > 32). If no number is read in, the integer is 0. Change the symbol if necessary (starting from step 2).
  • If the number of integers exceeds the range of 32-bit signed integers[ − 2 31 -2^{31} −231, 2 31 2^{31} 231 − 1], you need to truncate this integer to keep it within this range. Specifically, less than − 2 31 -2^{31} The integer of − 231 should be fixed to − 2 31 -2^{31} − 231, greater than 2 31 2^{31} The integer of 231 − 1 should be fixed to 2 31 2^{31} 231 − 1
  • Returns an integer as the final result.

be careful:

  • The white space character in this question only includes the white space character ''.
  • Do not ignore any characters other than the leading space or the rest of the string after the number

Tips:

  • 0 <= s.length <= 200
  • s consists of English letters (uppercase and lowercase), numbers (0-9), ',' + ',' - 'and'. ' form

thinking

After analyzing the topic, we think there are two difficulties in this topic

  • Judge whether the current character is what we need and what operations should be performed
  • Judge whether a number is out of bounds within the storage range of 32-bit signed integers

Judge current character operation

We judge whether the current character is what we need and what operations should be performed. The characters we need are divided into the following three categories

  • Leading space. Characters other than spaces are not allowed in front of spaces. If they appear, the program stops
  • Sign. Characters other than spaces are not allowed before the sign. If they appear, the program stops
  • Number. Characters other than spaces, signs and numbers are not allowed before numbers. If they appear, the program stops

In other cases, the program stops

This is easy to judge. The procedure is as follows

if(s[i] < '0' || s[i] > '9'){
                if((s[i] == ' ' && i == 0) || (s[i] == ' ' && s[i-1] == ' '))
                    continue;
                else if((s[i] == '-' && i == 0) || (s[i] == '-' && s[i-1] == ' '))
                    sign = -1;
                else if((s[i] == '+' && i == 0) || (s[i] == '+' && s[i-1] == ' '))
                    continue;
                else break;
            }

Judgment crossing

The idea of this question is the same as that of integer inversion. See details for details leetcode – integer inversion_ CSDN blog . The code is as follows

else{
    int temp = s[i] - '0';
    if(ans > 214748364 || (ans == 214748364 && temp > 7))
        return 2147483647;
    if(ans < -214748364 || (ans == -214748364 && temp > 8))
        return -2147483648;
    ans = ans * 10 + temp * sign;
}

Complete code

class Solution {
public:
    int myAtoi(string s) {
        int sign = 1; //Symbol
        int ans = 0; //answer
        
        for(int i = 0; i < s.size(); i++){
            if(s[i] < '0' || s[i] > '9'){
                if((s[i] == ' ' && i == 0) || (s[i] == ' ' && s[i-1] == ' '))
                    continue;
                else if((s[i] == '-' && i == 0) || (s[i] == '-' && s[i-1] == ' '))
                    sign = -1;
                else if((s[i] == '+' && i == 0) || (s[i] == '+' && s[i-1] == ' '))
                    continue;
                else break;
            }
            else{
                int temp = s[i] - '0';
                if(ans > 214748364 || (ans == 214748364 && temp > 7))
                    return 2147483647;
                if(ans < -214748364 || (ans == -214748364 && temp > 8))
                    return -2147483648;
                ans = ans * 10 + temp * sign;
            }
        }
        return ans;
    }
};

Topics: C++ Algorithm leetcode