lc3. 无重复字符的最长子串

class Solution:
    def lengthOfLongestSubstring(self, s: str) -> int:
        if len(s) < 2:
            return len(s)
        max_length = 0
        i = 0
        slide_window = {}
        while i < len(s):
       # 遇到重复元素时,滑动窗口重置到重复元素的下一个元素 
if s[i] in slide_window.keys(): max_length = max(max_length, len(slide_window)) i = slide_window[s[i]] + 1 slide_window.clear() slide_window[s[i]] = i i += 1 return max(max_length, len(slide_window))

 

posted @ 2022-05-05 22:49  stronger_el  阅读(37)  评论(0)    收藏  举报