滑动窗口
3. 无重复字符的最长子串

class Solution {
public int lengthOfLongestSubstring(String s) {
int res = 0;
int l = 0;
int r = 0;
Map<Character, Integer> map = new HashMap<>(); // 记录字符出现个数,始终要维持 1
char[] chars = s.toCharArray();
for (int i = 0; i < chars.length; i++) {
r++; // 无脑向前
char temp = chars[i];
if (map.getOrDefault(temp, 0) > 0) {
for (int j = l; j < i; j++) {
if (chars[j] == temp) { // 找到了前面要加入的
l++;
break;
} else {
l++;
map.put(chars[j], 0); // 清空前方字符
}
}
}
map.put(temp, 1); // 新加的数,放入 map 中
res = Math.max(res, r - l); // r 是从 1 开始的(当前数的下一个位置)
}
return res;
}
}
76. 最小覆盖子串