题解 | #表示数值的字符串#
表示数值的字符串
https://www.nowcoder.com/practice/e69148f8528c4039ad89bb2546fd4ff8
暴力全部模拟。写的时候能看出来很多重复的。不想改了。
class Solution: def isNumeric(self, str: str) -> bool: i, length = 0, len(str) while str[i] == " ": i += 1 if i == length: return False if str[i] in "+-": i += 1 if i == length: return False # 小数中的'.'+数字的情况,包括科学计数 if str[i] == ".": i += 1 if i == length: # 时刻牢记i+=1之后要记得判断是否到尾部了 return False if not "0" <= str[i] <= "9": return False while "0" <= str[i] <= "9": i += 1 if i == length: return True if str[i] == " ": while str[i] == " ": i += 1 if i == length: return True return False if str[i] in "eE": i += 1 if i == length: return False if str[i] in "+-": i += 1 if i == length: return False if not "0" <= str[i] <= "9": return False while "0" <= str[i] <= "9": i += 1 if i == length: return True if str[i] == " ": while str[i] == " ": i += 1 if i == length: return True return False return False return False # 数字开头的情况三种(整数、数字+'.'、数字+'.'+数字) if "0" <= str[i] <= "9": while "0" <= str[i] <= "9": i += 1 if i == length: return True # 整数的情况,包括科学计数 if str[i] == " ": while str[i] == " ": i += 1 if i == length: return True return False if str[i] in "eE": i += 1 if i == length: return False if str[i] in "+-": i += 1 if i == length: return False if not "0" <= str[i] <= "9": return False while "0" <= str[i] <= "9": i += 1 if i == length: return True if str[i] == " ": while str[i] == " ": i += 1 if i == length: return True return False return False # 小数中的数字+'.'的情况,包括科学计数 if str[i] == ".": i += 1 if i == length: return True if str[i] == " ": while str[i] == " ": i += 1 if i == length: return True return False if str[i] in "eE": i += 1 if i == length: return False if str[i] in "+-": i += 1 if i == length: return False if not "0" <= str[i] <= "9": return False while "0" <= str[i] <= "9": i += 1 if i == length: return True if str[i] == " ": while str[i] == " ": i += 1 if i == length: return True return False return False # 小数中的数字+'.'+数字的情况,包括科学计数 if not "0" <= str[i] <= "9": return False while "0" <= str[i] <= "9": i += 1 if i == length: return True if str[i] == " ": while str[i] == " ": i += 1 if i == length: return True return False if str[i] in "eE": i += 1 if i == length: return False if str[i] in "+-": i += 1 if i == length: return False if not "0" <= str[i] <= "9": return False while "0" <= str[i] <= "9": i += 1 if i == length: return True if str[i] == " ": while str[i] == " ": i += 1 if i == length: return True return False return False return False return False return False