提示:
滑动窗口一般会用到while循环
class Solution:
# 滑动窗口双指针
def lengthOfLongestSubstring(self, s: str) -> int:
occ = set()
n = len(s)
right, res = -1 , 0
for left in range(n):
if left != 0:
occ.remove(s[left-1])
while right+1 < n and s[right+1] not in occ:
occ.add(s[right+1])
right += 1
res = max(res, right-left+1)
return res
参考:leetcode题解