不必死磕 多次提交,每次修改代码即可
class Solution(object):
def isNumber(self,s):
s=s.strip()
#去除空格
if not s:
return False
if s[0]=='+' or s[0]=='-':
s=s[1:]
if not s or s[0]=='+' or s[0]=='-' or s[0]=='.' and len(s)==1:
return False
dot=0
Ee=0
num=0
for i in range(len(s)):
char=s[i]
if '0'<=char<='9':
num+=1
elif char=='+' or char=='-':
if not (s[i-1]=='e' or s[i-1]=='E'):
return False
elif char=='.':
dot+=1
if dot>1:
return False
if Ee:
return False
elif char=='e' or char=='E':
Ee+=1
if num==0:
return False
if i==0:
# E e 开头
return False
if i+1==len(s):
# E e 结尾
return False
if Ee>1:
return False
if s[i+1]=='+' or s[i+1]=='-':
if i+2==len(s):return False
else:
return False
return True