/** * @Description 滑动窗口问题 * map用来存储,一个字符所对应的位置的下一个位置 * 这样当出现了一个重复字符的时候,left 直接等于 map.value就相当于窗口左边界滑过了重复字符, * 窗口内就没有重复的字符了 * @Date 2022/1/9 8:15 下午 * @Author wuqichuan@zuoyebang.com **/class Solution { public int lengthOfLongestSubstring(String s) { int left = 0; int res = 0; HashMap<Character,Integer> map = new HashMap<>(); for (int right = 0; right < s.length(); right++) { Character c = s.charAt(right); if(map.containsKey(c)){ left = Math.max(left,map.get(c)); } res = Math.max(res,right - left + 1); map.put(c,right+1); } return res; }}