65. Valid Number [LeetCode]

/**************************************************************************
 * 
 * 65. [Valid Number](https://leetcode.com/problems/valid-number/)
 * 
 * For example, all the following are valid numbers: 
 * ["2", "0089", "-0.1", "+3.14", "4.", "-.9", "2e10", "-90E3", "3e+7", "+6e-1", "53.5e93", "-123.456e789"], 
 * while the following are not valid numbers: ["abc", "1a", "1e", "e3", "99e2.5", "--6", "-+3", "95a54e53"]. 
 * Given a string s, return true if s is a valid number.
 * 
 **************************************************************************/

///
///
/// Approach 1: 
bool isNumber(char * s){
    if (NULL == s) return false;
    int len = strlen(s);
    int index = 0; //index of s
    int nums = 0;  //nums of digits
    
    while (s[index] == ' ') 
        index++;
    
    while (s[index] == '+' || s[index] == '-') 
        index++;
    
    while (isdigit(s[index])) 
        index++, nums++;
    
    if (s[index] == '.') 
        index++;
    
    while (isdigit(s[index])) 
        index++, nums++;
    
    if (nums == 0) 
        return false;//小数点前后均没有数字、e前面没有数字 
    
    if (s[index] == 'e' || s[index] == 'E') {
        index++;
        nums = 0;
        
        if (s[index] == '+' || s[index] == '-') 
            index++;
        
        while (isdigit(s[index])) 
            index++, nums++;
        
        if (nums == 0) 
            return false; //e后面没数字
    }
    
    while (s[index] == ' ') 
        index++;
    
    return len == index;
}

///
///
/// Approach 2:  Use Deterministic Finite Automata method
bool isNumber(char* s) {
    if (NULL == s) return false;
    int len = strlen(s);
    typedef enum {
        INVALID,    // 0
        SPACE,      // 1
        SIGN,       // 2
        DIGIT,      // 3
        DOT,        // 4
        EXPONENT,   // 5
        NUM_INPUTS  // 6
    } InputType;
    //二维数组
    const int transitionTable[][NUM_INPUTS] = {
        -1, 0,  3,  1,  2,  -1,  // next states for state 0
        -1, 8,  -1, 1,  4,  5,   // next states for state 1
        -1, -1, -1, 4,  -1, -1,  // next states for state 2
        -1, -1, -1, 1,  2,  -1,  // next states for state 3
        -1, 8,  -1, 4,  -1, 5,   // next states for state 4
        -1, -1, 6,  7,  -1, -1,  // next states for state 5
        -1, -1, -1, 7,  -1, -1,  // next states for state 6
        -1, 8,  -1, 7,  -1, -1,  // next states for state 7
        -1, 8,  -1, -1, -1, -1,  // next states for state 8
    };
    int state = 0;
    for (int i = 0; i < len; i++) {
        char ch = s[i];
        InputType inputType = INVALID;
        if (isspace(ch))
            inputType = SPACE;
        else if (ch == '+' || ch == '-')
            inputType = SIGN;
        else if (isdigit(ch))
            inputType = DIGIT;
        else if (ch == '.')
            inputType = DOT;
        else if (ch == 'e' || ch == 'E')
            inputType = EXPONENT;
        // Get next state from current state and input symbol
        state = transitionTable[state][inputType];
        // Invalid input
        if (state == -1) return false;
    }
    // If the current state belongs to one of the accepting (final) states,
    // then the number is valid
    return state == 1 || state == 4 || state == 7 || state == 8;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

luuyiran

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值