I read some online solution using FSM. The code is clear and easy to understand. But I cannot figure out how the 9 states are determined. Can anyone explain to me in detail why it come up with this 9 states? No more no less? I don't know the way to abstract situations into FSM states, which I think is important in the future if I met similar problems that can be solved with FSM very easily.
Just write the regex pattern of valid number string,
then transform the regex pattern to NFA or DFA.
It's may not easy for someone not familiar with FSM, but you can use some tools like this: http://hackingoff.com/compilers/regular-expression-to-nfa-dfa