题目:
3.无重复字符的最长子串
思路:
滑动窗口
我用windows.pop(0)代替了
left索引
class Solution:
def lengthOfLongestSubstring(self, s: str) -> int:
# left = 0
right = 0
windows = []
most_length_child = 0
while right < len(s):
windows.append(s[right])
# print(windows)
while len(windows) != len(set(windows)):
windows.pop(0)
# left += 1
if len(windows) >= most_length_child:
most_length_child = len(windows)
right+= 1
return 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 = 0
windows = []
max_substr = 0
while right < len(s):
windows.append(s[right])
right += 1
while len(windows) != len(set(windows)):
windows.pop(0)
max_substr = max(max_substr, len(windows))
return max_substr