classSolution: deflengthOfLongestSubstring(self, s: str) -> int: result = 0 left = right = 0 a_dict = {} n = len(s) while right < n: if s[right] notin a_dict: # 字典中未查到该字符出现过 a_dict[s[right]] = right # 记录索引位置 right += 1 else: if (right - left) > result: # 更新最大值 result = right - left if left < a_dict[s[right]] + 1: left = a_dict[s[right]] + 1 a_dict.pop(s[right]) if right - left > result: # 更新最大值 result = right - left return result
classSolution: deflengthOfLongestSubstring(self, s: str) -> int: result = 0 left = right = 0 a_dict = {} n = len(s) for right inrange(n): if s[right] in a_dict: # 字典中查到该字符出现过 left = max(left, a_dict[s[right]]) result = max(result, right - left + 1) a_dict[s[right]] = right + 1# 记录索引位置 return result