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