关闭

LeetCode 65 Valid Number

标签: leetcode
255人阅读 评论(0) 收藏 举报
分类:

问题

https://leetcode.com/problems/valid-number/

解法

构造状态转移表, 根据输入转移

class Solution {
public:
    Solution()
    {
        memset(stateTable, 0, sizeof(stateTable));
        stateTable[Start_s][Blank] = Start_s;
        stateTable[Start_s][Signal] = PreInterge_s;
        stateTable[Start_s][Decimal] = NoIntDecimal_s;
        stateTable[Start_s][Number] = Interge_s;

        stateTable[PreInterge_s][Number] = Interge_s;
        stateTable[PreInterge_s][Decimal] = NoIntDecimal_s;

        stateTable[Interge_s][Number] = Interge_s;
        stateTable[Interge_s][Decimal] = Decimal_s;
        stateTable[Interge_s][Exp] = Exp_s;
        stateTable[Interge_s][Blank] = End_s;

        stateTable[Decimal_s][Number] = DecimalPart_s;
        stateTable[Decimal_s][Exp] = Exp_s;
        stateTable[Decimal_s][Blank] = End_s;

        stateTable[NoIntDecimal_s][Number] = DecimalPart_s;

        stateTable[DecimalPart_s][Number]= DecimalPart_s;
        stateTable[DecimalPart_s][Exp]= Exp_s;
        stateTable[DecimalPart_s][Blank] = End_s;

        stateTable[Exp_s][Signal] = PreExpPart_s;
        stateTable[Exp_s][Number] = ExpPart_s;

        stateTable[PreExpPart_s][Number] = ExpPart_s;

        stateTable[ExpPart_s][Number] = ExpPart_s;
        stateTable[ExpPart_s][Blank] = End_s;

        stateTable[End_s][Blank] = End_s;
    }
    typedef enum STATE {Err_s = 0, Start_s, PreInterge_s, Interge_s, Decimal_s, NoIntDecimal_s, DecimalPart_s, Exp_s, PreExpPart_s, ExpPart_s, End_s , StateNum } State;
    typedef enum SYMBOL { Blank =0, Number, Decimal, Exp, Signal, Other, SymbolNum} Symbol;
    bool isNumber(string s) {
        State state = Start_s;
        for (int i=0; i< s.size(); ++i)
        {
            Symbol input;
            if ('0'<=s[i] && s[i] <= '9')
                input = Number;
            else if (s[i] ==' ')
                input = Blank;
            else if (s[i] == 'e' || s[i] =='E')
                input = Exp;
            else if (s[i] == '.')
                input = Decimal;
            else if (s[i] == '+' || s[i] == '-')
                input = Signal;
            else
                return false;

            state = stateTable[state][input];
            if (state == Err_s)
                return false;

        }
        state = stateTable[state][Blank];
        return state == End_s;
    }
private:
    State stateTable[20][10];
};
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:21513次
    • 积分:1358
    • 等级:
    • 排名:千里之外
    • 原创:108篇
    • 转载:35篇
    • 译文:4篇
    • 评论:0条
    文章分类