边工作边刷题:70天一遍leetcode: day 27-1

Valid Number

要点:用几个boolean flag来区分状态:spa,exp,dot,num。为了简化程序,单向pass开始的空格和’+’/’-’。这些boolean状态flag只是在之后的main loop里标记状态

  • spa:用来标记空格不在字符中出现,所以当遇到空格并且spa为False,ok,如果非空格但是spa为True,false。所以这里用if … elif ...来处理
  • num: 一旦在num里,除了dot不能出现其他字符。另外num用来作为返回值和排除exp没有number的情况(“.”)
  • exp:一旦在exp里,不能再出现dot,
  • dot:exp和dot有一点不同,前者必须在num里,后者不用
  • 再次出现’+’/‘-’:不一定是第二次,意思是在main loop里num之后,那么左边必须为exp
class Solution(object):
    def isNumber(self, s):
        """
        :type s: str
        :rtype: bool
        """
        num = False
        exp = False
        dot = False
        spa = False
        
        i = 0
        while i<len(s) and s[i]==' ':
            i+=1
        
        if i<len(s) and (s[i]=='+' or s[i]=='-'):
            i+=1
        
        while i<len(s):
            if s[i]==' ':
                spa = True
            elif spa:
                return False
            elif ord(s[i])-ord('0')<=9 and ord(s[i])-ord('0')>=0:
                num = True
            elif s[i]=='.':
                if dot or exp: 
                    return False
                dot = True
            elif s[i]=='e':
                if not num or exp:
                    return False
                exp = True
                num = False
            elif s[i]=='+' or s[i]=='-':
                if s[i-1]!='e':
                    return False
            else:
                return False
            i+=1
        return num
                
        
posted @ 2016-04-21 10:51  absolute100  阅读(129)  评论(0编辑  收藏  举报