LeetCode65——Valid Number

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.


注意:"-1.", "46.e3" 一类是一个有效的数字, "6e6.5"一类不是有效的数字。

C++: http://en.cppreference.com/w/cpp/language/floating_literal

Python: https://docs.python.org/2/reference/lexical_analysis.html#floating-point-literals

Java: https://docs.oracle.com/javase/specs/jls/se7/html/jls-3.html#jls-3.10.2

这个方法比较好理解,但是速度比较慢,可以尝试下DFA(有限状态机)。

LeetCode65——Valid Number_第1张图片

下面的方法是逐个判断:

class Solution {
private:
    bool isSpace(char c){ return c==' ';}
    bool isSgn(char c){ return c=='+'||c=='-';}
    bool isDot(char c){ return c=='.';}
    bool isNum(char c){ return c<='9'&&c>='0';}
    bool isE(char c){ return c=='e'||c=='E';}

public:
    bool isNumber(string s) {
        int pos=0;
        bool haveNum = false;

        while ( pos


你可能感兴趣的:(LeetCode,C++,算法,c++,leetcode,string)