- 无重复字符的最长子串
解题思路
滑动窗口
代码
class Solution {
public int lengthOfLongestSubstring(String s) {
Map<Character,Integer> window=new HashMap<Character,Integer>();
int templength=0;
int left=0,right=0;
//char[] array=s.toCharArray();
while(right < s.length()){
//c是将移入窗口的数字
char c=s.charAt(right);
right++;
// 如果满足条件就将 c移入窗口中,并进行窗口内数据的一系列更新
window.put(c, window.getOrDefault(c, 0)+1);
while(window.get(c)>1){
char d=s.charAt(left);
left++;
window.put(d,window.get(d)-1);
}
templength=Math.max(templength,right-left);
}
return templength;
}
}