[Leetcode] Valid number

Source: Internet
Author: User

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 on the reload button to reset your cod E definition.

Hide TagsMath String

Idea: Divided into 3 end, before the decimal point, after the decimal point, after E, marked 1 2 3 segment, 1 and 3 segments can have a sign, 1 and 2 can have any 1 segment is empty, but not all empty

As a whole, it's a lot of detail to think about.

Tesecase

intMain () {solution SL;#if1cout<< 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;#endifcout<< Sl.isnumber ("+.8") <<Endl; return 0;}

The last Code

classSolution {enumAllow_pos_neg {allow_none, Allow_pos, Allow_neg, allow_all};  Public:        BOOLIsint (stringSenumAllow_pos_neg allow = Allow_none,BOOLAllowEmpty =false) {size_t n=s.size (); cout<<"s:\t"<< s <<Endl; intStart =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(inti = start; I < n; i++)            {                if(S[i] >='0'&& S[i] <='9')                    ; Else                    return false; }            return true; }        BOOLIsnumber (strings) {size_t n=s.size (); if(n = =0)                return false; intStart =0, end = n1; //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-- ; }            stringNewstr = S.substr (start, End-start +1); size_t Founde= Newstr.find ('e'); //cout << "newstr\t" << newstr << Endl; //cout << "founde\t" << founde<< Endl;            if(Founde! =string:: NPOs)//find ' e '            {                if(Founde = =0|| Founde = = Newstr.size ()-1)                    return false; size_t Foundpoint= Newstr.find ('.'); //cout << "foundpoint\t" << foundpoint << Endl;                if(Foundpoint! =string:: NPOs)//find '. '                {                    if(Foundpoint >=Founde)return false; if(Foundpoint = =0)//ex ". 1e34"                        returnIsint (Newstr.substr (foundpoint+1, founde-foundpoint-1), Allow_none)&& Isint (Newstr.substr (founde+1), Allow_all); if(Foundpoint = = founde-1)//ex "3.e40"                        returnIsint (Newstr.substr (0, Foundpoint), Allow_all)&& Isint (Newstr.substr (founde+1), Allow_all); returnIsint (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                    returnIsint (Newstr.substr (0, Founde), Allow_all)&& Isint (Newstr.substr (founde+1), Allow_all); }            Else{size_t Foundpoint= Newstr.find ('.'); //cout << "foundpoint\t" << foundpoint << Endl;                if(Foundpoint! =string:: NPOs)//find '. '                {                    if(Foundpoint = =0)                        returnIsint (Newstr.substr (foundpoint+1), Allow_none); Else if(Foundpoint = = Newstr.size ()-1)                        returnIsint (Newstr.substr (0, Foundpoint), Allow_all); Else                        returnIsint (Newstr.substr (0, Foundpoint), Allow_all,true)                            && Isint (Newstr.substr (foundpoint+1), Allow_none); }                Else                    returnisint (Newstr, Allow_all); }        }};

[Leetcode] Valid number

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.