请实现一个函数用来判断字符串是否表示数值(包括整数和小数)。例如,字符串"+100"、“5e2”、"-123"、“3.1416”、“0123"都表示数值,但"12e”、“1a3.14”、“1.2.3”、“±5”、"-1E-16"及"12e+5.4"都不是。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/biao-shi-shu-zhi-de-zi-fu-chuan-lcof
思路分析:
对正确和不正确的形式进行分析,发现不正确的情况要少一些,因此对不正确的情况进行判断
- e的次数不能出现两次,e不能出现在最后的位置,e后面不能出现小数点
- 小数点不能出现两次
- 正负号出现的位置只能是首位和e后面
- 出现了除e以外的字符
class Solution:
def isNumber(self, s: str) -> bool:
s = s.strip()
met_dot = met_e = met_digit = False
for i, char in enumerate(s):
if char in ('+', '-'):
if i > 0 and s[i-1] != 'e' and s[i-1] != 'E':
return False
elif char == '.':
if met_dot or met_e:
return False
met_dot = True
elif char == 'e' or char == 'E':
if met_e or not met_digit:
return False
met_e, met_digit = True, False # e后必须接,所以这时重置met_digit为False,以免e为最后一个char
elif char.isdigit():
met_digit = True
else:
return False
return met_digit