Share my 8ms C solution with annotation, just go from the begining to the end.


  • 1
    A
    bool isNumber(char* s) {
        bool dot=0;
        bool e=0;
        bool num=0;
        while (*s==' ') s++;//allow ' 's as start
        if (*s=='-'||*s=='+') s++;//only one sign symbol
        while (*s) {
            switch(*s) {
                case '.':
                    if (dot) return false;//only one dot
                    else dot=1;
                    s++;
                    break;
                case 'e':
                    if (e) return false;//only one 'e'
                    if (!num) return false;
                    else {
                        e=1; dot=1; num=0;//no dot after 'e'
                        s++;
                        if (*s=='-'||*s=='+') s++;//only one sign symbol
                    }
                    break;
                case '0':case'1':case'2':case'3':case'4':case'5':case'6':case'7':case'8':case'9':
                    num=1;
                    s++;
                    break;
                case ' ':
                    do {s++;} while (*s==' ');//allow ' 's as tail
                    if (*s) return false;
                    break;
                default://other characters
                    return false;
                    break;
            }
        }
        if (!num) return false;//must have numbers
        return true;
    }

Log in to reply
 

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