python solution


  • 0
    D
    class Solution(object):
        def isNumber(self, s):
            """
            :type s: str
            :rtype: bool
            """
            
    
            #0.5e50 True
            #2.0e30.0 False
            #.3 True
            #.5e20 True
            #2e false
            #e2false
            #2.e7. false
            # 005047e+6 true
            s = s.strip()
            if len(s) == 0:
                return False
            if s == '.':
                return False
            index = 0 
            dot_e_q_count = 0 # dot count before e
            e_q_count = 0 # numbers befor e
            e_h_count = 0 # numbers after e
            e_count = 0 # numbers of e
            dot_e_h_count = 0 # dot count after e
            e_q_sign = 0#sign number before e
            e_h_sign = 0#sign number after e
            while index < len(s):
                if s[index] == '+' or s[index] == '-':
                    if e_count == 0:
                        if e_q_sign > 0:
                            return False
                        elif index == 0:
                            e_q_sign += 1
                            index += 1
                        else:
                            return False
                    else:
                        if e_h_sign > 0:
                            return False
                        elif s[index - 1] == 'e':
                            e_h_sign += 1
                            index += 1  
                        else:
                            return False
                elif s[index] == '.':
                    if dot_e_q_count == 0:
                        if e_count == 0:
                            index += 1
                            dot_e_q_count += 1
                        else:
                            return False
                    else:
                        return False
                        # if e_count == 0:
                            # return False
    #                     else:
    #                         else:
    #                             dot_e_h_count += 1
    #                             index += 1
                        
                elif s[index] == 'e':
                    if e_count > 0:
                        return False
                    else:
                        if e_q_count == 0:
                            return False
                        else:
                            e_count += 1
                            index += 1
                elif ord(s[index]) - ord('0') >= 0 and ord(s[index]) - ord('0') <= 9:
                    index += 1
                    if e_count == 0:
                        e_q_count += 1
                    else:
                        e_h_count += 1
                else:
                    return False
            if e_count == 0:
                if e_q_count > 0:
                    return True
                else:
                    return False
            else:
                if e_h_count == 0:
                    return False
                else:
                    return True
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.