Valid Number

Validate if a given string is numeric.

Some examples:

"0" => true

" 0.1 " => true

"abc" => false

"1 a" => false

"2e10" => true


Pay attention to ' ', '+'/ '-', 'e', '.'

    def isNumber(self, s):
        """
        :type s: str
        :rtype: bool
        """
        isValid = False
        i = 0
        n = len(s)
        while i<n and s[i] == ' ':
            i += 1
        if i<n and s[i] in ['+','-']:
            i += 1
        while i<n and s[i].isdigit():
            i += 1
            isValid = True
        if i<n and s[i] == '.':
            i += 1
        while i<n and s[i].isdigit():
            i += 1
            isValid = True
        if i<n and isValid and s[i] =='e':
            isValid = False
            i += 1
            if i < n and s[i] in ['+','-']:
                i += 1
            while i<n and s[i].isdigit():
                i += 1
                isValid = True
        while i < n and s[i] == ' ':
            i += 1
        return isValid if i == n else False

results matching ""

    No results matching ""