My Python Answer, Just A DFA :P


  • 0
    B
    class Solution:
        
        digits = set(map(chr, apply(range, [ord('0'), ord('9')+1])))
        #digits = set("0123456789")
        
        # @param s, a string
        # @return a boolean
        def isNumber(self, s):
            r'[-+]?((\d+\.)|(\d+\.\d+)|(\.\d+)|(\d+))(e[-+]?\d+)?'
            ns = s.strip()
            
            def f1(x):
                '[-+]?'
                if type(x) is bool:
                    return x
                else:
                    if 0 == len(x):
                        return False
                    else:
                        if x[0] in ('-', '+'):
                            return x[1:]
                        else:
                            return x
                        
            def f2(x):
                '((\d+\.)|(\d+\.\d+)|(\.\d+)|(\d+))(e..'
                if type(x) is bool:
                    return x
                else:
                    xs = x.split('e')
                    sxs = (set(xs[0]) - set('.'))
                    if xs[0].count('.') < 2 and 0 < len(sxs) and sxs.issubset(self.digits) and x.count('e') < 2:
                        if len(xs) == 1:
                            return True
                        else:
                            if 0 < len(xs[1]):
                                return xs[1]
                            else:
                                return False
                    else:
                        return False
                        
            def f3(x):
                '\d+'
                if type(x)is bool:
                    return x
                else:
                    return (0 < len(x) and set(x).issubset(self.digits))
                    
            return f3(f1(f2(f1(ns))))

Log in to reply
 

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