用滑动窗口
主要注意滑动窗口的滑动的条件
用两个指针,从最左边开始,右指针向右移动,并且右指针移动后,将对应字符存入set中,当右指针指向的字符已经存在set中时,就移动左指针,然后set删去左指针对应的字符,直到set中没有右指针对应的字符时,左指针停止移动
public class Offer48 {
public int lengthOfLongestSubstring(String s) {
char[] chars = s.toCharArray();
HashSet set = new HashSet<>();
int left = 0,right = 0;
int max = 0;
for (; right < chars.length; right++){
while (set.contains(chars[right])){//移动左边界
set.remove(chars[left]);
left++;
}
set.add(chars[right]);//将窗口新加入的字符加入set中去
max = Math.max(right-left+1,max);
}
return max;
}
}



