我太难了,俩小时。。。。。。 class Solution(object): def isNumber(self, s): """ :type s: str :rtype: bool """ s= s.strip() for i in range(len(s)): if s[i] not in ‘ e0123456789+-. ‘ : return False if len(s)1 : return False
我太难了,俩小时。。。。。。
class Solution(object): def isNumber(self, s): """ :type s: str :rtype: bool """ s=s.strip() for i in range(len(s)): if s[i] not in ‘e0123456789+-.‘: return False if len(s)<1: return False if len(s)==1: if s[0] not in ‘0123456789‘: return False if s[0] in ‘e‘ or s[-1] in ‘e+-‘: return False if s.count(‘e‘)>1: return False if s.count(‘e‘)==1: if s.count(‘.‘)>1: return False elif s.count(‘.‘)==1: #print(‘gygufyufu‘) if s[0]==‘.‘ and s[1]==‘e‘: return False if s.index(‘e‘)==s.index(‘.‘)-1: return False if s.index(‘.‘)>s.index(‘e‘): return False if s.index(‘.‘)<s.index(‘e‘): if s[s.index(‘.‘)+1] in ‘+-‘: return False if s[-1]!=‘.‘: return True else: return False if s.count(‘+‘)+s.count(‘-‘)>2: return False elif s.count(‘+‘)+s.count(‘-‘)==2: if s[0] in ‘+-‘ and s[s.index(‘e‘)+1] in ‘+-‘: return True else: return False elif s.count(‘+‘)+s.count(‘-‘)==1: if s[0] in ‘+-‘ or s[s.index(‘e‘)+1] in ‘+-‘: return True else: return False else: if s.count(‘+‘)+s.count(‘-‘)>2: return False elif s.count(‘+‘)+s.count(‘-‘)==2: if s[0] in ‘+-‘ and s[s.index(‘e‘)+1] in ‘+-‘: return True else: return False elif s.count(‘+‘)+s.count(‘-‘)==1: if s[0] in ‘+-‘ : if s[1]==‘e‘: return False else: return True elif s[s.index(‘e‘)+1] in ‘+-‘ and len(s)>s.index(‘e‘)+2: return True else: return False else: if s.count(‘.‘)>1: return False elif s.count(‘.‘)==1: if s.count(‘+‘)+s.count(‘-‘)>1: return False elif s.count(‘+‘)+s.count(‘-‘)==1: if s[0] in ‘+-‘ : if len(s)==2: if s[1]==‘.‘: return False else: return True elif len(s)>2: if s[1]==‘.‘ and s[2] not in ‘0123456789‘: return False else: return True else: return True else: return False if s[s.index(‘.‘)-1] in ‘+-‘: return False else: if s.count(‘+‘)+s.count(‘-‘)>1: return False elif s.count(‘+‘)+s.count(‘-‘)==1: if s[0] in ‘+-‘: return True else: return False return True执行用时 :28 ms, 在所有 Python 提交中击败了70.92%的用户 内存消耗 :11.7 MB, 在所有 Python 提交中击败了24.00%的用户
执行用时为 12 ms 的范例 class Solution(object): def isNumber(self, s): """ :type s: str :rtype: bool """ if not s: return False s = s.strip() sci = s.split(‘e‘) if len(sci) > 2: return False if len(sci) == 1: return False if not sci[0] else self.is_num(s) signs = [‘-‘, ‘+‘] sci[1] = sci[1] if not sci[1] or sci[1][0] not in signs else sci[1][1:] return self.is_num(sci[0]) and self.is_int(sci[1]) def is_num(self, s): if not s: return False signs = [‘-‘, ‘+‘] parts = s.split(‘.‘) if len(parts) > 2: return False parts[0] = parts[0] if not parts[0] or parts[0][0] not in signs else parts[0][1:] if len(parts) == 1: return self.is_int(parts[0]) if not parts[0] and self.is_int(parts[1]): return True if not parts[1] and self.is_int(parts[0]): return True return self.is_int(parts[0]) and self.is_int(parts[1]) def is_int(self, s): if not s: return False for i in range(0, len(s)): if not s[i].isdigit(): return False return True
没仔细看。。。
——2019.10.11