Solution in C++ using a state machine


  • 0
    A
    class Solution {
    public:
        bool emptyBeforeDecimal = true;
        bool emptyAfterDecimal = true;
        bool emptyExponent = true;
        enum State {BEGIN, SIGN, BEG_DECIMAL, DECIMAL, INT, SCIENTIFIC, END, BEG_SCI, SCI_SIGN, ERROR};
        enum Sign {POSITIVE, NEGATIVE};
        bool isNumber(string s) {
            State state = BEGIN;
            const char *str = s.c_str();
            for (int i = 0; i < s.length(); ++i) {
                switch (state) {
                    case BEGIN : state = processBegin(str[i]);
                        break;
                    case SIGN : state = processSign(str[i]);
                        break;
                    case END : state = processEnd(str[i]);
                        break;
                    case BEG_DECIMAL : state = processBegDecimal(str[i]);
                        break;
                    case DECIMAL : state = processDecimal(str[i]);
                        break;
                    case INT : state = processInt(str[i]);
                        break;
                    case SCIENTIFIC : state = processScientific(str[i]);
                        break;
                    case SCI_SIGN: state = processSciSign(str[i]);
                        break;
                    case BEG_SCI: state = processBegSci(str[i]);
                        break;
                    case ERROR: return false;
                    default: return false;
                }
            }
            if (state == END || state == INT || state == DECIMAL || state == BEG_DECIMAL) {
                if (emptyAfterDecimal && emptyBeforeDecimal) {
                    return false;
                }
                return true;
            }
           if (state == SCIENTIFIC) {
               if (emptyAfterDecimal && emptyBeforeDecimal) {
                    return false;
               }
               if (emptyExponent) {
                   return false;
                }
                return true;
            }
            return false;            
        }
        
        State processBegin(const char c) {
            if (c == ' ') {
                return BEGIN;
            }
            if (c == '-' || c == '+') {
                return SIGN;
            }
            if (int(c) >= 48 && int(c) < 58) {
                emptyBeforeDecimal = false;
                return INT;
            }
            if (c == '.') {
                return BEG_DECIMAL;
            }
            return ERROR;
        }
        
        State processSign(const char c) {
            if (c == ' ') {
                return ERROR;
            }
            if (int(c) >= 48 && int(c) < 58) {
                emptyBeforeDecimal = false;
                return INT;
            }
            if (c == '.') {
                return BEG_DECIMAL;
            }
            return ERROR;
        }
        
        State processInt(const char c) {
            if (int(c) >= 48 && int(c) < 58) {
                return INT;
            }
            if (c == '.') {
                return BEG_DECIMAL;
            }
            if (c == 'e') {
                return BEG_SCI;
            }
            if (c == ' ') {
                return END;
            }
            return ERROR;
        }
        
        State processScientific(const char c) {
            if (int(c) >= 48 && int(c) < 58) {
                return SCIENTIFIC;
            }
            if (c == ' ') {
                return END;
            }
            return ERROR;
        }
        
        State processBegSci(const char c) {
            if (int(c) >= 48 && int(c) < 58) {
                emptyExponent = false;
                return SCIENTIFIC;
            }
            if (c == '-' || c == '+') {
                return SCI_SIGN;
            }
            return ERROR;
        }
        
        State processSciSign(const char c) {
            if (int(c) >= 48 && int(c) < 58) {
                emptyExponent = false;
                return SCIENTIFIC;
            }
            if (c == ' ') {
                return END;
            }
            return ERROR;
        }
        
        
        State processBegDecimal(const char c) {
            if (int(c) >= 48 && int(c) < 58) {
                emptyAfterDecimal = false;
                return DECIMAL;
            }
            if (c == 'e') {
                return BEG_SCI;
            }
            if (c == ' ') {
                return END;
            }
            return ERROR;
        }
        
        State processDecimal(const char c) {
            if (int(c) >= 48 && int(c) < 58) {
                return DECIMAL;
            }
            if (c == 'e') {
                return BEG_SCI;
            }
            if (c == ' ') {
                return END;
            }
            return ERROR;
        }
        
        State processEnd(const char c) {
            if (c == ' ') {
                return END;
            }
            return ERROR;
        }
    };
    
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.