Code first

```
class Solution {
public:
bool isNumber(string str) {
int state=0, flag=0; // flag to judge the special case "."
while(str[0]==' ') str.erase(0,1);//delete the prefix whitespace
while(str[str.length()-1]==' ') str.erase(str.length()-1, 1);//delete the suffix whitespace
for(int i=0; i<str.length(); i++){
if('0'<=str[i] && str[i]<='9'){
flag=1;
if(state<=2) state=2;
else state=(state<=5)?5:7;
}
else if('+'==str[i] || '-'==str[i]){
if(state==0 || state==3) state++;
else return false;
}
else if('.'==str[i]){
if(state<=2) state=6;
else return false;
}
else if('e'==str[i]){
if(flag&&(state==2 || state==6 || state==7)) state=3;
else return false;
}
else return false;
}
return (state==2 || state==5 || (flag&&state==6) || state==7);
}
};
```

**DFA**

**Thank @unknowcs, he came up with a brilliant provement in comments that making this a perfect DFA！**

It's just some states changes depend on inputs only.

There 8 state in my states in my DFA.

Below is my **DFA transition diagram**.

or click picture here

DFA transition diagram

There are **5** kind of inputs in my DFA:

digit : number **0-9** for

+,- : operator **+** or **-**(negative or positive)

exp: **e**

dot: **.**

other: you can return **false** Immediately

**4 final States** in my DFA transition diagram : **s2, s6, s7, s8**

**If the state change to final state at last, return true.**

s2 can accept digits only : +1 -23432 123 and etc

s5 can accept exp expression: +2.4e+12 3e9 and etc

s6 can accept decimals end with dot: 1. -42. and etc

(careful, what if there exist only one dot "."

**** I use a variable flag judging weather there existing numbers. cause 0. and .0 is valid and . is invalid ****

)

s7 can accept decimals: +12.23, 87., 132

It is clear how DFA works in my pictures. We just need to handle the inputs, and update the state according to DFA.