65. Valid Number (Hard) - TengnanYao/daily_leetcode GitHub Wiki

class Solution(object):
    def isNumber(self, s):
        """
        :type s: str
        :rtype: bool
        """
        dots = 0
        es = 0
        numbers = 0
        n = len(s)
        if s[-1] in "+-Ee":
            return False
        if s[0] in "Ee":
            return False
        for i, c in enumerate(s):
            if c in "+-":
                if not (i == 0 or i > 0 and s[i - 1] in "eE"):
                    return False
            elif c.isdigit():
                numbers += 1
            elif c in "Ee":
                if (s[i + 1].isdigit() or s[i + 1] in "+-") and (s[i - 1].isdigit() or s[i - 1] == "."):
                    es += 1
                else:
                    return False
            elif c == ".":
                if not es and (i != 0 or i == 0 and i < n - 1 and s[i + 1].isdigit()):
                    dots += 1
                else:
                    return False
            else:
                return False
            if dots > 1 or es > 1:
                return False
        return numbers > 0