3.9 ValidNumber

Notes:
  Validate if a given string is numeric.
  Some examples:
  "0" => true
  " 0.1 " => true
  "abc" => false
  "1 a" => false
  "2e10" => true
  Note: It is intended for the problem statement to be ambiguous. You should gather all
  requirements up front before implementing one.
 
  Solution: This finite-state machine solution. Learn from fuwutu & snakeDling.
  */
class Solution {
public:
    bool isNumber_1(const char *s) {
        enum InputType {INVALID, SPACE, SIGN, DIGIT, DOT, EXPONENT};
        int transitionTable[][SPACEEND] = 
        { /* 0   1   2   3   4   5  */
             0,  1,  2,  3,  4,  0, // 0: INVALID
             0,  1,  2,  3,  4,  0, // 1: SPACE
             0,  0,  0,  3,  4,  0, // 2: SIGN
             0,  6,  0,  3,  7,  5, // 3: DIGIT
             0,  0,  0,  7,  0,  0, // 4: DOT
             0,  0,  2,  8,  0,  0, // 5: EXPONENT
             0,  6,  0,  0,  0,  0, // 6: END WITH SPACE
             0,  6,  0,  7,  0,  5, // 7: DOT AND DIGIT
             0,  6,  0,  8,  0,  0, // 8: END WITH SPACE OR DIGIT
        };
        
        InputType last = INVALID;
        while (*s != '\0')
        {
            InputType state = INVALID;
            if (*s == ' ')
                state = SPACE;
            else if (isdigit(*s))
                state = DIGIT;
            else if (*s == '+' || *s == '-')
                state = SIGN;
            else if (*s == 'e')
                state = EXPONENT;
            else if (*s == '.')
                state = DOT;
            last = (InputType) transitionTable[last][state];
            if (last == INVALID) return false;
            s++;
        }
        bool validFinal[] = {0, 0, 0, 1, 0, 0, 1, 1, 1};
        return validFinal[last];
    }
    bool isNumber_2(const char *s) {
        bool dot = false, digit = false, exp = false;
        while (*s  == ' ') ++s;
        if (*s == '-' || *s == '+') ++s;
        if (*s == 0) return false;
        for (;*s != '\0' && *s != ' '; ++s) {
            if (isdigit(*s)) digit = true;
            else if (*s == 'e' || *s == 'E') {
                if (exp == true || digit == false || *(s+1) ==' ' || *(s+1) =='\0') return false;
                exp = true;
            } else if (*s == '.') {
                if (dot == true || exp == true) return false; 
                if (digit == false && (*(s+1) ==' ' || *(s+1) =='\0')) return false;
                dot = true;
            } else if (*s == '-' || *s == '+') {
                if (*(s+1) == ' ' || *(s+1) == '\0') return false;
                if (*(s-1) != 'e' && *(s-1) != 'E') return false;
            } else return false;
        }
        while (*s  == ' ') ++s;
        return *s == '\0';
    }
};


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值