Valid Number @python

Valid Number

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.

直接上代码,特别是解法二,引入 re 模块匹配,很精简强大!

 1 class InputType:
 2     INVALID = 0
 3     SPACE = 1
 4     SIGN =2
 5     DIGIT =3
 6     DOT = 4
 7     EXPONENT =5
 8
 9 class Solution:
10     # @param s, a string
11     # @return a boolean
12     def isNumber(self, s):
13         transition_table = [[-1,  0,  3,  1,  2, -1],     # next states for state 0
14                             [-1,  8, -1,  1,  4,  5],     # next states for state 1
15                             [-1, -1, -1,  4, -1, -1],     # next states for state 2
16                             [-1, -1, -1,  1,  2, -1],     # next states for state 3
17                             [-1,  8, -1,  4, -1,  5],     # next states for state 4
18                             [-1, -1,  6,  7, -1, -1],     # next states for state 5
19                             [-1, -1, -1,  7, -1, -1],     # next states for state 6
20                             [-1,  8, -1,  7, -1, -1],     # next states for state 7
21                             [-1,  8, -1, -1, -1, -1]]     # next states for state 8
22
23         state = 0
24         for char in s:
25             inputType = InputType.INVALID
26             if char.isspace():
27                 inputType = InputType.SPACE;
28             elif char == ‘+‘ or char == ‘-‘:
29                 inputType = InputType.SIGN
30             elif char in ‘0123456789‘:
31                 inputType =InputType.DIGIT
32             elif char  == ‘.‘:
33                     inputType = InputType.DOT
34             elif char == ‘e‘ or char ==‘E‘:
35                 inputType = InputType.EXPONENT
36
37             state = transition_table[state][inputType]
38             if state == -1:
39                 return False
40         return state==1 or state==4 or state ==7 or state==8
41
42     def isNumber2(self,s):
43         import re
44         return bool(re.match("^\s*[\+\-]?((\d+(\.\d*)?)|\.\d+)([eE][+-]?\d+)?\s*$",s))

时间: 2024-08-15 02:53:37

Valid Number @python的相关文章

[leetcode]Valid Number @ Python

原题地址:http://oj.leetcode.com/problems/valid-number/ 题意:判断输入的字符串是否是合法的数. 解题思路:这题只能用确定有穷状态自动机(DFA)来写会比较优雅.本文参考了http://blog.csdn.net/kenden23/article/details/18696083里面的内容,在此致谢! 首先这个题有9种状态: 0初始无输入或者只有space的状态1输入了数字之后的状态2前面无数字,只输入了dot的状态3输入了符号状态4前面有数字和有do

Valid Number

package cn.edu.xidian.sselab;/** * title:Valid Number * content: * Validate if a given string is numeric. * Some examples: * "0" => true * " 0.1 " => true * "abc" => false * "1 a" => false * "2e10&q

LeetCode --- 65. Valid Number

题目链接:Valid Number 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 statemen

[LintCode] Valid Number 验证数字

Validate if a given string is numeric. Have you met this question in a real interview? Yes Example "0" => true " 0.1 " => true "abc" => false "1 a" => false "2e10" => true LeetCode上的原题,请参见我之

【leetcode】Valid Number

Valid Number 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 am

Valid Number leetcode java

题目: 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 ambigu

LeetCode: Valid Number 解题报告

Valid NumberValidate if a given string is numeric. Some examples:"0" => true" 0.1 " => true"abc" => false"1 a" => false"2e10" => trueNote: It is intended for the problem statement to be ambi

【leetcode刷题笔记】Valid Number

Validate if a given string is numeric. Some examples:"0" => true" 0.1 " => true"abc" => false"1 a" => false"2e10" => true 题解:题目不难,就是有点麻烦,要注意的地方很多,总结一下: 前面和后面的空格要用s.trim()去掉: 前导的'+'和'-'号需要忽略:

LeetCode: Valid Number [066]

[题目] 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 ambig