Wins the offer - a string representing a number

Title Description

Implement function used to determine whether a string represents a value (including integer and fractional). For example, the string "+100", "5e2", "- 123", "3.1416" and "-1E-16" shows the value. But "12e", "1a3.14", "1.2.3", "+ - 5" and "12e + 4.3" neither.

Thinking

This nothing to say, pay attention to the border like

class Solution:
    def isNumeric(self, s):
        if len(s) == 0:
            return False
        point = False
        nums = []
        for c in s:
            if c =="+" or c=="-":
                if len(nums)!=0 and (nums[-1]!="E" and nums[-1]!="e"):
                    return False
                else:
                    nums.append(c)
            elif c == "e" or c == "E":
                if len(nums)==0:
                    return False
                elif ord(nums[-1]) > ord('9') or ord(nums[-1])<ord('0'):
                    return False
                else:
                    nums.append(c)
                    point = True
            elif c == '.':
                if point or len(nums) == 0:
                    return False
                else:
                    point = True
                    nums.append(c)
            elif c >'9' or c< '0':
                return False
            else:
                nums.append(c)
            
        if nums[-1] in ["e",'E','.','+',"-"]:
            return False
        return True

 

Published 82 original articles · won praise 2 · Views 4346

Guess you like

Origin blog.csdn.net/qq_22498427/article/details/104978896