滑动窗口通用解法。不赘述。贴代码:
public int lengthOfLongestSubstring(String s) { Map<Character,Integer> window=new HashMap<>(); int left=0,right=0; int length=0; while(right<s.length()) { char r=s.charAt(right); right++; window.put(r,window.getOrDefault(r,0)+1); while(window.get(r)>1) { char d=s.charAt(left); left++; window.replace(d,window.get(d)-1); } length=Math.max(length,right-left); } return length; }