‘e’ can only appear after digits and ‘.’ cannot appear after ‘e’. In other words, the character before ‘e’ and after ‘e’ must be both digits. (e3 and 3e are both invalid)
‘+’ and ‘-‘ can only be the first character of the string or the next character of ‘e’.
‘.’ can appear only one time and before e.
classSolution:defisNumber(self, s):"""
:type s: str
:rtype: bool
"""
s = s.strip()
isDot, isDigit, isE =False,False,Falsefor i, x inenumerate(s):if x=='e':ifnot isDigit or isE:returnFalse
isDigit=False
isE=Trueelif x in"+-":ifnot i==0andnot s[i-1]=='e':returnFalseelif x=='.':if isDot or isE:returnFalse
isDot=Trueelif x.isdecimal():
isDigit=Trueelse:returnFalsereturn isDigit