C++ state machine solution (12ms), not sure how to improve further


  • 0
    T

    This is not the fastest, but I am not sure how to improve further.

    class Solution {
    public:
        bool isNumber(string s) {
            // \s*[+|-]*[0-9]*(.[0-9]*)*(e[+|-]*[0-9]+)*\s*
            
            int state = 0;
            for(int i = 0; i < s.size(); ++ i) {
                char c = s[i];
                
                switch(state) {
                    case 0:
                        if(c == ' ')
                            state = 0;
                        else if(c == '+' || c == '-')
                            state = 1;
                        else if(c >= '0' && c <= '9')
                            state = 2;
                        else if(c == '.')
                            state = 30;
                        else
                            return false;
                        break;
                    case 1:
                        if(c >= '0' && c <= '9')
                            state = 2;
                        else if(c == '.')
                            state = 30;
                        else
                            return false;
                        break;
                    case 2: // good
                        if(c >= '0' && c <= '9')
                            state = 2;
                        else if(c == '.')
                            state = 3;
                        else if(c == 'e' || c == 'E')
                            state = 4;
                        else if(c == ' ')
                            state = 9;
                        else
                            return false;
                        break;
                    case 3: // good
                        if((c >= '0' && c <= '9'))
                            state = 31;
                        else if(c == 'e' || c == 'E')
                            state = 4;
                        else if(c == ' ')
                            state = 9;
                        else
                            return false;
                        break;
                    case 30:
                        if((c >= '0' && c <= '9'))
                            state = 31;
                        else
                            return false;
                        break;
                    case 31: // good
                        if((c >= '0' && c <= '9'))
                            state = 31;
                        else if(c == 'e' || c == 'E')
                            state = 4;
                        else if(c == ' ')
                            state = 9;
                        else
                            return false;
                        break;
                    case 4:
                        if(c == '+' || c == '-')
                            state = 41;
                        else if(c >= '0' && c <= '9')
                            state = 42;
                        else
                            return false;
                        break;
                    case 41:
                        if(c >= '0' && c <= '9')
                            state = 42;
                        else
                            return false;
                        break;
                    case 42: // good
                        if(c >= '0' && c <= '9')
                            state = 42;
                        else if(c == ' ')
                            state = 9;
                        else
                            return false;
                        break;
                    case 9:
                        if(c == ' ')
                            state = 9;
                        else
                            return false;
                        break;
                    default:
                        return false;
                }
    //            cout << state << "\n";
            }
            return (state == 2 || state == 3 || state == 31 || state == 42 || state == 9);
        }
    };

  • 0
    P

    My solution is essentially the same as yours, except that I am using a class enum for clarity. And somehow, my solution takes 8ms ... Maybe using integers is actually slower, even more so because they are not contiguous?

    Also, according to my tests, you are not allowed to use 'E' for the exponent. For example, "1E3" returns false.


Log in to reply
 

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