LINTCODE——有效数字(待改进)

来源:互联网 发布:所有香烟条形码数据库 编辑:程序博客网 时间:2024/05/17 08:43

LINTCODE——有效数字

思路:代码写的稀烂,暂时没想好更好的处理分类,虽然AC了,但是还是mark一下;
注意”+.1e4”为TRUE;

class Solution {public:    /*     * @param s: the string that represents a number     * @return: whether the string is a valid number     */    bool isNumber(string &s) {        // write your code here        if(s.empty())            return false;        int n = s.size();        bool begin = true ,mark = false ,mid = true,emid = true;        for( int i  = 0; i < n ;i++)        {            if(check(s[i]))            {                begin = false;                mark = true;            }            //开头处理‘-’‘+’‘ ’‘。’等情况            else if(begin)            {                if(s[i] == '-' || s[i] == '+')                {                    begin = false;                }                else if(s[i] == ' ')                    continue;                else if( (s[i] == '.') && mid == true)                    mid = false;                else                    return false;            }            //尾部空格一空到底            else if(s[i] == ' ')            {                //空格到底则为真                while(i < n && s[i] == ' ')                    i++;                if(i == n)                    return true;                else                    return false;            }            //中间只能是e或者是‘。’,如果是e那么开头结尾必须要有数字            else if(mid || emid)            {                if(s[i] == '.' && mid)                    mid = false;                else if(s[i] == 'e'&& emid)                {                    if(i+1<n&&check(s[i+1]))                        {                            mid = false;                            emid = false;                            continue;                        }                    else                        return false;                }                else                    return false;            }            else                return false;         }        return mark;    }    bool check(char a)    {        if( a >= '0' && a <= '9')            return true;        else            return false;    }};