My C 5ms solution


  • 0
    P
    bool isNumber(char *s) {
        if (!s) return false;
        while(s[0]==' ') ++s;
        while(s[strlen(s)-1]==' ') s[strlen(s)-1]='\0';
        bool canSign=true, canE=false, wasE=false, canDot=true, wasDot=false, aftE=false, wasNum=false;
        int n=strlen(s);
        for (int i=0; i<n;++i)
        {
            if ((s[i]=='+')||(s[i]=='-'))
                if (canSign)
                {
                    canSign = false;
                    canE = false;
                }
                else
                    return false;
            else
            if ((s[i]>='0')&&(s[i]<='9'))
            {
                canSign = false;
                canDot = true;
                canE = true;
                aftE = false;
                wasNum= true;
            }
            else
            if (s[i]=='e')
                if ((!canE)||(wasE))
                    return false;
                else
                {
                    wasE=true;
                    canSign=true;
                    aftE=true;
                }
            else
            if (s[i]=='.')
            {
                if ((!canDot)||(wasDot)||(wasE))
                    return false;
                else
                {
                    canSign = false;
                    wasDot = true;
                }
            }
            else
                return false;
        }
        return !aftE&&wasNum;
    }

Log in to reply
 

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