滑动窗口
public int lengthOfLongestSubstring1st(String s) {
int res = 0, left = 0, right = 0;
int n = s.length();
Set<Character> set = new HashSet<>();
while (right < n && left < n) {
if (!set.contains(s.charAt(right))) {
set.add(s.charAt(right++));
res = Math.max(res, right - left);
} else {
set.remove(s.charAt(left++));
}
}
return res;
}方法1:双端队列

方法2:双端队列
方法3:辅助数组+贪心


159.最多有两个不同字符的最长子串
方法1:Map统计数量+滑动窗口
方法2:Map标记位置+滑动窗口
方法3:滑动窗口+指针
340.最多有K个不同字符的最长子串
方法1:Map统计数量+滑动窗口
方法2:Map标记位置+滑动窗口
方法1:递归
方法1:滑动窗口


方法2:滑动窗口+统计
Follow up:返回所有符合条件的索引
Last updated