LeetCode 65 Valid Number

原创 2016年05月30日 21:41:43

问题

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];
};
版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

【题解】Leetcode.65.Valid Number (qduoj.162.莫妮提)

题目链接: Leetcode.65.Valid Number qduoj.162.莫妮提 题目大意 : 给出一个字符串,判断其是否是一个数字 题目没有给出具体要求...

LeetCode | 65. Valid Number

Validate if a given string is numeric.Some examples: "0" => true " 0.1 " => true "abc" => false "1 a...

leetcode-65-Valid Number

#include #include using namespace std; /* 判断合法数字。 有三种合法数字:(正、负) 1. 整数 2. 小数 .5也算合法 小数点的前面不能有小数...

LeetCode65——Valid Number

LeetCode65——Valid Number 这个题目给定的拿督评分是Hard,做了之后发现,它真正Hard的地方不在于这个题目有多难想,而是由多恶心 判断字符串是否是数字,由于题目的...

[Leetcode] 65. Valid Number 解题报告

题目: Validate if a given string is numeric. Some examples: "0" => true " 0.1 " => true "abc" => fals...

LeetCode --- 65. Valid Number

题目链接:Valid Number Validate if a given string is numeric. Some examples: "0" => true " 0.1 " =>...
  • makuiyu
  • makuiyu
  • 2015年03月15日 23:34
  • 578

【LeetCode】65. Valid Number

题目描述Validate if a given string is numeric.Some examples:"0" => true " 0.1 " => true "abc" => false "...

Valid Number(使用DFA,有图)

LeetCode题目 Validate if a given string is numeric.Some examples: “0” => true ” 0.1 ” => true “abc...

leetcode|Valid Number(65)

Validate if a given string is numeric. Some examples: "0" => true " 0.1 " => true "abc" => false "1...

Leetcode 65. Valid Number 验证数字 解题报告

1 解题思想这道题条条框框是在太多了,各种情况。。不过简略来说,正确的做法应该是: 1、熟悉数字的表述规则(可以看网上的,也可以看我代码的),这道题关键是要懂所有的数字规则。 2、对输入的数字首先...
  • MebiuW
  • MebiuW
  • 2016年05月08日 23:44
  • 3521
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:LeetCode 65 Valid Number
举报原因:
原因补充:

(最多只允许输入30个字)