16.无重复字符的最长子串
方法:滑动窗口
class Solution {
public int lengthOfLongestSubstring(String s) {
int n = s.length();
Set<Character> set = new HashSet<>();
int l = 0,ans = 0;
for(int i = 0; i < n;i++){
char ch = s.charAt(i);
while(set.contains(ch)){
set.remove(s.charAt(l));
l++;
}
set.add(ch);
ans = Math.max(ans, i- l + 1);
}
return ans;
}
}