题解 | #表示数值的字符串#
表示数值的字符串
https://www.nowcoder.com/practice/e69148f8528c4039ad89bb2546fd4ff8
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
#
# @param str string字符串
# @return bool布尔型
#
class Solution:
def isNumeric(self , str: str) -> bool:
# write code here
n = len(str)
index = 0
has_num = has_sign = has_e = has_dot = False
while index<n and str[index] == ' ':
index += 1
while index<n:
while index<n and '0'<=str[index]<='9':
index += 1
has_num = True
if index == n:
break
if str[index] == 'e' or str[index] == 'E':
if has_e or not has_num:
return False
has_e = True
has_num = has_sign = has_dot = False
elif str[index]=='+' or str[index]=='-':
if has_sign or has_num or has_dot:
return False
has_sign = True
elif str[index] == '.':
if has_dot or has_e:
return False
has_dot = True
elif str[index] == ' ':
break
else:
return False
index += 1
while index <n and str[index]==' ':
index += 1
return has_num and index == n

