https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/
    点击查看【bilibili】

    1. class Solution(object):
    2. def lengthOfLongestSubstring(self, s):
    3. # 定义左右指针
    4. left = 0
    5. right = 0
    6. # 定义输出的最大长度
    7. max = 0
    8. # 定义一个字典来存储当前字符串
    9. window = dict()
    10. while right < len(s):
    11. current_char = s[right]
    12. # .setdefault()
    13. # 如果键不存在于字典中,将会添加键并将值设为默认值。
    14. window.setdefault(current_char, 0)
    15. window[current_char] += 1
    16. # 往后走一位
    17. right += 1
    18. # 窗口中有重复字符时缩小窗口
    19. while window[current_char] > 1:
    20. left_char = s[left]
    21. # i往右移动一位
    22. left += 1
    23. # 更新字典中 current_char 的数量
    24. window[left_char] -= 1
    25. window_size = right - left
    26. if window_size > max:
    27. max = window_size
    28. return max