regex solution in python, just a few lines

  • 0
    def isNumber(self, s):
        	pat = [r'^[+|-]?\d*\.?\d+$', r'^[+|-]?\d+\.\d*$', r'^[+|-]?\d*\.?\d+e[+|-]?\d+$', r'^[+|-]?\d+\.?\d*e[+|-]?\d+$']
        	s = s.strip().lower()
        	for p in pat:
        		if re.match(p, s):
        			return True
        	return False

Log in to reply

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