美文网首页
剑指 Offer 第48题:最长不含重复字符的子字符串

剑指 Offer 第48题:最长不含重复字符的子字符串

作者: 放开那个BUG | 来源:发表于2022-08-07 12:47 被阅读0次

    1、前言

    题目描述

    2、思路

    使用滑动窗口的思路来做,滑动窗口必须有 left、right 两个边界。

    3、代码

    class Solution {
        public int lengthOfLongestSubstring(String s) {
            if(s == null || s.length() == 0){
                return 0;
            }
    
            Map<Character, Integer> window = new HashMap<>();
            int max = 0;
            for(int i = 0, j = 0; j < s.length(); j++){
                while(window.containsKey(s.charAt(j))){
                    window.remove(s.charAt(i++));
                }
    
                window.put(s.charAt(j), 1);
                // max = Math.max(max, window.size());
                max = Math.max(max, j - i + 1);
            }
    
            return max;
        }
    }
    

    相关文章

      网友评论

          本文标题:剑指 Offer 第48题:最长不含重复字符的子字符串

          本文链接:https://www.haomeiwen.com/subject/dtobbrtx.html