class Solution {
public int lengthOfLongestSubstring(String s) {
if(s.length()==0) return 0;
//双指针(r指针最先向右移动,遇到重复的l指针向右移动直到越过重复的r指针继续向右移动)
int l=0,r=0,res=0;
Map<Character,Integer> map = new HashMap<>();
while(l<=r){
char ch_r = s.charAt(r);
map.put(ch_r,map.getOrDefault(ch_r,0)+1);
while(map.get(ch_r)>1){
char ch_l = s.charAt(l);
map.put(ch_l,map.getOrDefault(ch_l,0)-1);
l++;
}
res = res > r-l+1? res:r-l+1;
r++;
if(r==s.length()) break;
}
return res;
}
}
原文:https://www.cnblogs.com/cstdio1/p/13334448.html