leetcode: Valid Number

leetcode Valid Number Problem Description:

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.

leetcode Valid Number Solution and Precautions:

This problem description is indeed fuzzy:  several special cases are

// 3e3.1 is false
// -1. is true
// +.1 is true
// 1.e3 is true

The solution is to build a DFA to process the string, the following is my DFA (please forgive my for the ugly drawing >_<), and at first I think come up with a regular expression first, then translate it into DFA would be easier for bug free code, but later on I found it more complicated, so one better just build the DFA directly.

Finite State Machine For Valid Number

Tips and Divergent thinking:



(转载本站文章请注明作者和出处 烟客旅人 sigmainfy — http://www.sigmainfy.com,请勿用于任何商业用途)

Written on May 1, 2013