题目描述
blablabla
样例
blablabla
算法1
() $O(n^2)$
blablabla
时间复杂度分析:blablabla
Python 代码
class Solution(object):
def isNumber(self, s):
"""
:type s: str
:rtype: bool
"""
i = 0
j = len(s) - 1
while i < len(s) and s[i] == ' ':
i += 1
while j > 0 and s[j] == ' ':
j -= 1
if i > j:
return False
s = s[i:j + 1] # 关键
if s[0] == '-' or s[0] == '+':
s = s[1:]
if len(s) == 0 or s[0] == '.' and len(s) == 1:
return False
e = 0
dot = 0
i = 0
while i < len(s): # 此处不可以用for loop 因为i+=1不会改变
if s[i] >= '0' and s[i] <= '9':
i += 1
continue
elif s[i] == '.':
dot += 1
if e or dot > 1: # 如果e后面出现'.' 或者多个'.' False
return False
i += 1
elif s[i] == 'e' or s[i] == 'E':
e += 1
if i + 1 == len(s) or i == 0 or e > 1 or i == 1 and s[0] == '.': # 如果e后面没有数字或.e False
return False
if s[i + 1] == '+' or s[i + 1] == '-': #如果e后面出现符号之后没有数字 False
if i + 2 == len(s):
return False
i += 1
i += 1
else:
return False
return True