zoukankan      html  css  js  c++  java
  • [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);
                }
    
    
            }
    };
  • 相关阅读:
    扫面线模板
    (动态规划、栈)leetcode 84. Largest Rectangle in Histogram, 85. Maximal Rectangle
    tmux 常见命令汇总
    leetcode 221
    leetcode 319 29
    (贪心)leetcode 392. Is Subsequence, 771. Jewels and Stones, 463. Island Perimeter
    leetcode 982 668
    Python import 同文件夹下的py文件的函数,pycharm报错
    Windows里Anaconda-Navigator无法打开的解决方案
    Windows下 gpu版 Tensorflow 安装
  • 原文地址:https://www.cnblogs.com/diegodu/p/4323587.html
Copyright © 2011-2022 走看看