65. Valid Number *HARD*

Posted ArgenBarbie

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了65. Valid Number *HARD*相关的知识,希望对你有一定的参考价值。

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.

bool isNumber(string s) {
    int l = s.length(), i;
    bool flag=0, point[2]={0,0}, num[2]={0,0};
    for(i=0; s[i] ==  ; i++);
    for(; i<l; i++)
    {
        if(s[i] ==  )
            break;
        if(s[i] == - || s[i] == +)
        {
            if(num[flag] == true || point[flag] == true)
                return false;
        }
        else if(isdigit(s[i]))
            num[flag] = true;
        else if(s[i] == .)
        {
            if(point[flag] || flag == true)
                return false;
            point[flag] = true;
        }
        else if(s[i] == e)
        {
            if(flag == 1)
                return false;
            if(num[0] == false)
                return false;
            flag = 1;
        }
        else
            return false;
    }
    for(;i<l;i++)
    {
        if(s[i]!= )
            return false;
    }
    if(!num[0] && !num[1])
        return false;
    if(flag && !num[1])
        return false;
    return true;
}

测试用例:

"0e" -- false

". 1" -- false

"-1." -- true

".-4" -- false

"+.8" -- true

"6e6.5" -- false

以上是关于65. Valid Number *HARD*的主要内容,如果未能解决你的问题,请参考以下文章

65. Valid Number

65. Valid Number

65. Valid Number

65. Valid Number

65. Valid Number

65. Valid Number