My C code accepted with 8 ms


  • 3
    K
    bool isNumber(char* s) {
        char c;
        //in : have integer <==>fr : have fraction
        bool in = false, fr = false;
        //ein : left of e <==> eout : right of e
        bool ein = false, eout = false;
        //1. space
        while(isspace(*s))
        {
            s++;
        }
        //2. after space
        if((!isdigit(*s)) && (*s != 'e') && (*s != '.') && (*s != '+') && (*s != '-'))
        {
            return false;
        }
        //3. + -
        if((*s == '+') || (*s == '-'))
        {
            s++;
        }
        //4. integer part
        if(isdigit(*s))
        {
            in = true;
            ein = true;
        }
        while(isdigit(*s))
        {
            s++;
        }
        //5. '.' and 'e'
        if((*s) == '.')
        {
            s++;
            if(isdigit(*s))
            {
                fr = true;
            }
            if((!fr) && (!in))
            {
                return false;
            }
            while(isdigit(*s))
            {
                s++;
            }
            ein = true;
        }
        if(*s == 'e')
        {
            s++;
            if((*s == '-') || (*s == '+'))
            {
                s++;
            }
            if(isdigit(*s))
            {
                eout = true;
            }
            if((!ein) || (!eout))
            {
                return false;
            }
            while(isdigit(*s))
            {
                s++;
            }
        }
      
        //6. ending space
        while(isspace(*s))
        {
            s++;
        }
        return (*s == '\0') ? true : false;
        
    }

Log in to reply
 

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