题目:
3.无重复字符的最长子串
思路:
滑动窗口
我用windows.pop(0)代替了left索引
class Solution:def lengthOfLongestSubstring(self, s: str) -> int:# left = 0right = 0windows = []most_length_child = 0while right < len(s):windows.append(s[right])# print(windows)while len(windows) != len(set(windows)):windows.pop(0)# left += 1if len(windows) >= most_length_child:most_length_child = len(windows)right+= 1return most_length_child
照着模板写,关键是第二个while处的条件
int left = 0, right = 0;while (right < s.size()) {window.add(s[right]);right++;while (valid) {window.remove(s[left]);left++;}}
第二次写的版本
class Solution:def lengthOfLongestSubstring(self, s: str) -> int:right = 0windows = []max_substr = 0while right < len(s):windows.append(s[right])right += 1while len(windows) != len(set(windows)):windows.pop(0)max_substr = max(max_substr, len(windows))return max_substr
