【方法一:滑动窗口】
在窗口[start,end)中遍历,始终保持窗口中的元素是无重复的。判断s[end]是否重复
若没有重复:向后移动end,子串长度加1
若有重复:向后移动start到重复位置的下一个位置。
int lengthOfLongestSubstring(string s) {
if(s.length()==1)return 1;
int maxlength = 0, length = 1;
int start =0, end = 1;
while (end<s.length())
{
length = 1;
for (int i = start; i < end; i++) {
if (s[i] == s[end]) {
start = i + 1;
break;
}
else {
length++;
}
}
end++;
maxlength = max(maxlength, length);
}
return maxlength;
}
滑动窗口题目:Leetcode
3. 无重复字符的最长子串
30. 串联所有单词的子串
76. 最小覆盖子串
159. 至多包含两个不同字符的最长子串
209. 长度最小的子数组
239. 滑动窗口最大值
567. 字符串的排列
632. 最小区间
727. 最小窗口子序列