8. String to Integer (atoi)] [finite state machine

8. String to Integer (atoi)

answer:

        Since the number is limited to int 32 bits in order to simplify the algorithm uses long int 64 bits processing algorithm Compile the principle of the finite state machine, the initial state is 0.

        0 Status: encountered space character, the state does not change, still 0;

                      Encountered alphabetic character / non-character numeric / non-negative number, 4 becomes inactive;

                      Numeric characters encountered, no symbol reading state becomes 1, and Flag tag read as a positive number;

                      Character encountered negative / positive sign character, symbol reading state has changed to 2, and was labeled Flag / read plus minus sign;

        1 status: encountered space character / letter / negative / any non-numeric characters, indicating the end of the reading, it becomes the end of the state 3;

                      Encountered numeric characters, readings continue to show, is still state 1;

        2 Status: encountered space character / letter / negative / any non-numeric characters, show false readings becomes inactive 4;

                      Encountered numeric characters indicate the start of reading negative / positive, read status becomes 1;

        3 State: terminated, stops and returns valid data conversion;

        4 states: inactive, stopped transformed and returns invalid data 0;
 

Specific processing methods and algorithms for each state of the implementation details, please refer to the source code:

class Solution {
public:
    int myAtoi(string str) {
        int len = str.size();
        char ch;
        int state = 0; // 状态初始为0;
        int signFlag; // 数字符号状态;
        long int res = 0;
        for(int i = 0; i < len; ){
            ch = str[i];
            if(state == 0){
                if(ch == ' '){
                    state = 0;
                    i++;
                }
                else if(isDigit(ch)){
                    signFlag = 1; // 正数读取
                    state = 1;
                }
                else if(ch == '+' || ch == '-'){
                    signFlag = ch == '+';
                    state = 2;
                    i++;
                }
                else{
                    state = 4;
                }
            }
            else if(state == 1){
                if(!isDigit(ch)){
                    state = 3;
                }
                else{
                    state = 1;
                    res = res * 10 + ch - '0';
                    if(signFlag){
                        if(res > 0x7fffffff){ //正数上限判断
                            res = 0x7fffffff;
                            state = 3;
                        }
                    }
                    else{
                        if(res > 0x80000000){ //负数下限判断
                            res = 0x80000000;
                            signFlag = 1;   //由于res被置为最大负数,因此在后面intres不能再取负
                            state = 3;
                        }
                    }
                    i++;
                }
            }
            else if(state == 2){
                if(!isDigit(ch)){
                    state = 4;
                }
                else{
                    state = 1;
                }
            }
            else if(state == 3){
                break;
            }
            else if(state == 4){
                res = 0;
                break;
            }
        }
        int intres = res;
        if(!signFlag){
            intres = -res;
        }
        return intres;
    }
private:
    bool isDigit(char ch){
        return (ch >= '0' && ch <= '9');
    }
};

The results show:

Results of the:

by

Show details

When execution: 0 ms, defeated 100.00% of all users to submit in C ++

Memory consumption: 8.5 MB, defeated 78.27% of all users to submit in C ++

发布了62 篇原创文章 · 获赞 9 · 访问量 7818

Guess you like

Origin blog.csdn.net/qq_40491305/article/details/104002470