Code30 string conversion integer (atoi)

topic

leetcode8. String conversion integer (atoi)
Please implement a myAtoi(string s) function to convert a 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 first character (assuming it has not reached the end of the character) is positive or negative, 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 in the next character until the next non-numeric character is reached or the end of the input is reached. The rest of the string will be ignored.
  • Convert the numbers read in the previous steps to integers (ie, "123" -> 123, "0032" -> 32). If no number is read, the integer is 0. Change the symbol if necessary (from step 2).
  • If the integer number exceeds the 32-bit signed integer range [−231, 231 − 1], the integer needs to be truncated to keep it within this range. Specifically, integers smaller than −231 should be fixed to −231 and integers larger than 231 − 1 should be fixed to 231 − 1.
  • Return an integer as the final result.

note:

  • The blank characters in this question only include the space character ''.
  • Except for the leading space or the rest of the string after the number, do not ignore any other characters.

Code

  • Ideas
    • Sequential read processing
    • If numbers,'-','+' appear, then non-digit characters appear again, and the traversal is stopped directly. A sign is required here, suppose it is nonnum_need_stop;
  • Code
// C++
class Solution {
    
    
public:
    int myAtoi(string s) {
    
    
        int64_t res = 0;
        bool nonnum_need_stop=false; // 
        int flag = 1;
        int32_t min = std::numeric_limits<int32_t>::min();
        int32_t max = std::numeric_limits<int32_t>::max();
        for(int i=0; i< s.size(); ++i){
    
    
            if ((s[i] - '0' >=0) && (s[i] - '0' <=9)){
    
    
                res = res * 10 + s[i] - '0';
                if (flag * res > max){
    
    
                    return max;
                } else if (flag * res < min){
    
    
                    return min;
                } else{
    
    
                    nonnum_need_stop=true;
                }
            }else if (nonnum_need_stop){
    
    
                break;
            }else {
    
    
                if (s[i]== ' '){
    
    
                    continue;
                }
                else if(s[i]== '-'){
    
    
                    flag=-1;
                    nonnum_need_stop=true;
                }
                else if(s[i]== '+'){
    
    
                    nonnum_need_stop=true;
                } else{
    
    
                    break;
                }           
            }            
        }
        return res * flag;
    }
};

Guess you like

Origin blog.csdn.net/luoshabugui/article/details/113107339