题目
    3.无重复字符的最长子串

    思路
    滑动窗口
    我用windows.pop(0)代替了left索引

    1. class Solution:
    2. def lengthOfLongestSubstring(self, s: str) -> int:
    3. # left = 0
    4. right = 0
    5. windows = []
    6. most_length_child = 0
    7. while right < len(s):
    8. windows.append(s[right])
    9. # print(windows)
    10. while len(windows) != len(set(windows)):
    11. windows.pop(0)
    12. # left += 1
    13. if len(windows) >= most_length_child:
    14. most_length_child = len(windows)
    15. right+= 1
    16. return most_length_child

    照着模板写,关键是第二个while处的条件

    1. int left = 0, right = 0;
    2. while (right < s.size()) {
    3. window.add(s[right]);
    4. right++;
    5. while (valid) {
    6. window.remove(s[left]);
    7. left++;
    8. }
    9. }

    第二次写的版本

    1. class Solution:
    2. def lengthOfLongestSubstring(self, s: str) -> int:
    3. right = 0
    4. windows = []
    5. max_substr = 0
    6. while right < len(s):
    7. windows.append(s[right])
    8. right += 1
    9. while len(windows) != len(set(windows)):
    10. windows.pop(0)
    11. max_substr = max(max_substr, len(windows))
    12. return max_substr