无重复字符的最长子串
2025-12-27 本文已影响0人
何以解君愁
class Solution {
public int lengthOfLongestSubstring(String s) {
Map<Character,Integer> map = new HashMap<>();
int left = 0,right = 0;
int max = 0;
while(right < s.length()){
if(map.getOrDefault(s.charAt(right),0) != 0){
map.put(s.charAt(left),map.getOrDefault(s.charAt(left),0) - 1);
left++;
}else{
map.put(s.charAt(right),map.getOrDefault(s.charAt(right),0) + 1);
right++;
max = Math.max(max,right - left);
}
}
return max;
}
}