def lengthOfLongestSubstring(self, s):
"""
:type s: str
:rtype: int
"""
if not s:
return 0
temp = set()
n = len(s)
count = 0
left = 0
res = 0
for i in range(n):
while s[i] in temp: # 不用列表,集合的实现是基于哈希表的,所以集合的查找速度比列表快
temp.remove(s[left])
left += 1
count -= 1
temp.add(s[i])
count += 1
if res < count:
res = count
return res
temp 用列表的查找速度会很慢,这与它的底层实现有关系。
参照https://blog.csdn.net/weixin_37720172/article/details/78636314