赞
踩
有限状态机:https://blog.csdn.net/suwei19870312/article/details/12094233
- class Solution(object):
- def isNumber(self, s):
- """
- :type s: str
- :rtype: bool
- """
- if not s:
- return False
- INVALID = 0
- SPACE = 1
- SIGN = 2
- DIGIT = 3
- DOT = 4
- E = 5
-
- transition = [
- [-1,0,3,1,2,-1],
- [-1,8,-1,1,4,5],
- [-1,-1,-1,4,-1,-1],
- [-1,-1,-1,1,2,-1],
- [-1,8,-1,4,-1,5],
- [-1,-1,6,7,-1,-1],
- [-1,-1,-1,7,-1,-1],
- [-1,8,-1,7,-1,-1],
- [-1,8,-1,-1,-1,-1]
- ]
-
- state = 0
- for i in s:
- if i == ' ':
- idx = SPACE
- elif i == '-' or i == '+':
- idx = SIGN
- elif i >= '0' and i <= '9':
- idx = DIGIT
- elif i == '.':
- idx = DOT
- elif i == 'E' or i == 'e':
- idx = E
- else:
- idx = INVALID
- state = transition[state][idx]
- if state == -1:
- return False
- return state == 1 or state == 4 or state == 7 or state == 8
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。