Topic:
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.
Code:
classSolution { Public: BOOLIsnumber (strings) {if(S.size () <1)return false; //escape space from begining and end intIndex_begin =0; intIndex_end = S.size ()-1; while(s[index_begin]==' '&& index_begin<s.size ()) + +Index_begin; while(s[index_end]==' '&& index_end>=0) --Index_end; //digit, DOT, sign, exp enumprechar{Nonpre, DIGIT, sign, EXP, DOT}; enumPrechar Prechar =Nonpre; BOOLHasdigit =false, hassign =false, Hasexp =false, Hasdot =false; intindex =Index_begin; for(; index <= index_end; + +index) { //Space if(s[index]==' ')return false; //Digit if(s[index]>='0'&& s[index]<='9') {Hasdigit =true; Prechar = DIGIT;Continue; } // Sign if(s[index]=='+'|| s[index]=='-' ){ if(Prechar==exp | | prechar==nonpre) {Prechar = sign; hassign =true;Continue; } Else{return false; } } //Exp if(s[index]=='e'|| s[index]=='E' ){ if((Prechar==digit | | prechar==dot) &&!hasexp && hasdigit) {Prechar = EXP; hasexp =true;Continue; } Else{return false; } } //Dot if(s[index]=='.' ){ if(!hasexp &&!hasdot && (prechar==digit | | prechar==sign | | prechar==nonpre)) {Prechar = DOT; Hasdot =true;Continue; } Else{return false; } } //illegal input char return false; } //end with digit or dot returnPrechar==digit | | (Prechar==dot &&hasdigit); }};
Tips
Mainly based on the idea of this blog (http://blog.unieagle.net/2012/11/06/leetcode title: valid-number/). On the basis of it, the logic is combed and simplified more systematically.
Ideas are as follows:
1. First eliminate the string end of space, this can simplify the judgment logic (as long as the next string in the space, it must not be a legal number)
2. Determine if dot ('. ') appears in the remaining string, sign (' + ', '-'), exp (' e ', ' e '), digit (0~9) is legal. The core logic of judgment is two:
A. What was the previous character (that is, Prechar in the code)
B. Dot,sign,exp,digit, have you ever seen
The advantage of this code logic is that if you look for two core judgment logic and tinker with the two core logic, you can constantly brush the test case until AC.
If there are similar problems in the future, the condition conditions are very many, but the input conditions are relatively fixed, and they need to be judged according to the serialized input criteria.
You can set a number of variables logical variables, and then by judging the values of several types of logical variables to go down, even if not a bug free, but always can complete the logic.
=====================================================
The code for this idea is very consice, as has been the practice of the finite state machine (FSM) (http://www.cnblogs.com/chasuner/p/validNumber.html), which has been seen online.
The FSM is very clear about how it works, but how the 0~8 state of the FSM is coming, and I don't understand it, so I have to choose a more general approach.
"Valid number" CPP