描述
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.
分析
有难度,需要构建状态机。画出来是这个样子的:
上面的图是用Graphviz画的,非常好用。贴一下上图的dot语言源码:
1 | digraph valid_number { |
时间复杂度O(n)
,空间O(1)
。
代码
Python
1 | class Solution(object): |