原题地址:力扣
用正则表达式匹配,复习了一下正则表达式。看了答案用有限自动状态机。
class Solution:
def isNumber(self, s: str) -> bool:
pattern = re.compile(r'^(\+|-)?((\d+(\.\d*)?)|(\.\d+))((e|E)(\+|-)?\d+)?$')
if pattern.match(s) != None:
return True
return False