• [LeetCode] 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.

    Update (2015-02-10):
    The signature of the C++ function had been updated. If you still see your function signature accepts a const char * argument, please click the reload button  to reset your code definition.

    Hide Tags
     Math String
     

    思路:分成3端,小数点前,小数点后,e后,标记为1 2 3段,1段和3段可以有正负号,1段和2段可以有任意1段为空,但不能都为空

    整体上不能,看是要考虑的细节特别多

    tesecase

    int main()
    {
    
        Solution sl;
    
    #if 1
        cout << sl.isNumber("0") << endl;
        cout << sl.isNumber("1 ") << endl;
        cout << sl.isNumber("1. ") << endl;
        cout << sl.isNumber(".1 ") << endl;
        cout << sl.isNumber(". ") << endl;
        cout << sl.isNumber(" 0.1") << endl;
        cout << sl.isNumber("abc") << endl;
        cout << sl.isNumber("1 a") << endl;
        cout << sl.isNumber("2.223e10") << endl;
        cout << sl.isNumber(" 005047e+6") << endl;
        cout << sl.isNumber("+++") << endl;
        cout << sl.isNumber("4e+") << endl;
        cout << sl.isNumber(".-4") << endl;
    #endif
        cout << sl.isNumber("+.8") << endl;
    
        return 0;
    }

    最后的code

    class Solution {
        enum ALLOW_POS_NEG {ALLOW_NONE, ALLOW_POS, ALLOW_NEG, ALLOW_ALL};
        public:
            bool isInt(string s, enum ALLOW_POS_NEG allow = ALLOW_NONE, bool allowEmpty = false)
            {
                size_t n = s.size();
    
                cout << "s:	" << s << endl;
                int start = 0;
                if(allow == ALLOW_POS)
                {
                    if(s[0] == '+')
                    {
                        start ++;
                    }
                }
                else if(allow == ALLOW_NEG)
                {
                    if(s[0] == '-')
                    {
                        start ++;
                    }
                }
                else if(allow == ALLOW_ALL)
                {
                    if(s[0] == '+' || s[0] == '-')
                    {
                        start ++;
                    }
                }
                else //ALLOW_NONE
                    ;
    
                if(start == n)
                {
                    if(allowEmpty == true)
                        return true;
                    else
                        return false;
                }
    
                for(int i = start; i < n; i++)
                {
                    if(s[i] >= '0' && s[i] <= '9')
                        ;
                    else
                        return false;
                }
                return true;
            }
    
            bool isNumber(string s)
            {
                size_t n = s.size();
                if(n == 0)
                    return false;
    
                int start = 0, end = n-1;
    
                // skip space
                while(start < n && s[start] == ' ')
                {
                    start++;
                }
    
                // skip '-'
                //if(s[start] == '-' || s[start] == '+')
                //    start ++;
    
                if(start == n)
                    return false;
    
                // skip space
                while(s[end] == ' ')
                {
                   end-- ;
                }
    
                string newStr = s.substr(start, end - start +1);
    
                size_t foundE = newStr.find('e');
    
                //cout << "newStr	" << newStr << endl;
                //cout << "foundE	" << foundE<< endl;
    
                if (foundE != string::npos)// find 'e'
                {
                    if(foundE == 0 || foundE == newStr.size()-1)
                        return false;
    
                    size_t foundPoint = newStr.find('.');
                    //cout << "foundPoint	" << foundPoint << endl;
                    if (foundPoint != string::npos)// find '.'
                    {
                        if(foundPoint >= foundE)
                            return false;
                        if(foundPoint == 0) // ex ".1e34"
                            return isInt(newStr.substr(foundPoint+1, foundE-foundPoint-1), ALLOW_NONE)
                                && isInt(newStr.substr(foundE+1), ALLOW_ALL) ;
                        if(foundPoint == foundE-1) // ex "3.e40"
                            return isInt(newStr.substr(0, foundPoint), ALLOW_ALL)
                                && isInt(newStr.substr(foundE+1), ALLOW_ALL) ;
    
                        return isInt(newStr.substr(0, foundPoint), ALLOW_ALL, true)
                            && isInt(newStr.substr(foundPoint+1, foundE-foundPoint-1), ALLOW_NONE)
                            && isInt(newStr.substr(foundE+1), ALLOW_ALL) ;
                    }
                    else
                        return isInt(newStr.substr(0, foundE), ALLOW_ALL)
                            && isInt(newStr.substr(foundE+1), ALLOW_ALL);
                }
                else
                {
                    size_t foundPoint = newStr.find('.');
                    //cout << "foundPoint	" << foundPoint << endl;
                    if (foundPoint != string::npos)// find '.'
                    {
                        if(foundPoint == 0)
                            return isInt(newStr.substr(foundPoint+1), ALLOW_NONE) ;
                        else if(foundPoint == newStr.size()-1)
                            return isInt(newStr.substr(0,foundPoint), ALLOW_ALL) ;
                        else
                            return isInt(newStr.substr(0, foundPoint), ALLOW_ALL, true)
                                && isInt(newStr.substr(foundPoint+1), ALLOW_NONE) ;
                    }
                    else
                        return isInt(newStr, ALLOW_ALL);
                }
    
    
            }
    };
  • 相关阅读:
    hadoop项目放在tomcat服务器中遇见的问题
    hadoop-hdfs间文件复制
    fastdfs扩容
    mysql-8.0修改密码方式
    同一个服务器启动两个redis服务记录!
    springboot+vue完美跨域 解决sessionId不一致问题
    docker 拉取fastDFS镜像
    共享锁、排他锁、互斥锁、悲观锁、乐观锁、行锁、表锁、页面锁、不可重复读、丢失修改、读脏数据
    linux下后台运行node-js项目
    概念解释:分组密码、流密码、对称密码、非对称密码
  • 原文地址:https://www.cnblogs.com/diegodu/p/4323587.html
Copyright © 2020-2023  润新知