76. 最小覆盖子串

76. 最小覆盖子串

滑动窗口

经典写法

java 复制代码
class Solution {
    public String minWindow(String s, String t) {
        HashMap<Character, Integer> window = new HashMap<>(), need = new HashMap<>();
        for(char c : t.toCharArray()) need.merge(c, 1, Integer::sum);
        int cnt = 0, start = 0, len = Integer.MAX_VALUE;

        for(int l = 0, r = 0; r < s.length(); r++){
            // 窗口增大的逻辑
            char c = s.charAt(r);
            if(need.containsKey(c)){
                window.merge(c, 1, Integer::sum);
                if(need.get(c).equals(window.get(c))) cnt++;
            }

            while(cnt == need.size()){  // 窗口何时缩小
                // 更新答案
                if(len > r - l + 1){
                    len = r - l + 1;
                    start = l;
                }
                // 窗口缩小的逻辑
                c = s.charAt(l++);
                if(need.containsKey(c)){
                    if(need.get(c).equals(window.get(c))) cnt--;
                    window.merge(c, -1, Integer::sum);
                }
            }
        }

        return len == Integer.MAX_VALUE ? "" : s.substring(start, start + len);
    }
}

简洁写法

java 复制代码
class Solution {
    public String minWindow(String s, String t) {
        char[] chs = s.toCharArray(), cht = t.toCharArray();
        int[] hash = new int[128];
        for(char c : cht) hash[c]--;
        int cnt = 0, m = cht.length;
        String res = "";

        for(int l = 0, r = 0; r < chs.length; r++){
            hash[chs[r]]++;
            if(hash[chs[r]] <= 0) cnt++;

            while(cnt == m && hash[chs[l]] > 0) hash[chs[l++]]--;
            
            if(cnt == m){
                if(res.equals("") || res.length() > r - l + 1){
                    res = s.substring(l, r + 1);
                }
            }
        }

        return res;
    }
}

一个坑

两个 Integer 相等比较应该用 equals,而不是 ==。

一个 Integer 和一个 int 比较,Integer 会自动拆箱,可以用 ==。

java 复制代码
class Solution {
    public String minWindow(String s, String t) {
        int i = 0;
        HashMap<Character, Integer> mapS = new HashMap<>();
        HashMap<Character, Integer> mapT = new HashMap<>();
        for(char c : t.toCharArray()){
            //mapT.merge(c, 1, Integer::sum);
            mapT.put(c, mapT.getOrDefault(c, 0) + 1);
        }
        int cnt = 0;
        int res = Integer.MAX_VALUE, left = 0, right = 0;

        for(int j = 0; j < s.length(); j++){
            char c = s.charAt(j);
            if(mapT.containsKey(c)){
                //mapS.merge(c, 1, Integer::sum);
                mapS.put(c, mapS.getOrDefault(c, 0) + 1);
                if(mapS.get(c).equals(mapT.get(c))){  // 一个坑:Integer 相等比较应该用 equals,而不是 ==
                    cnt++;
                }
            }
            
            while(cnt == mapT.size()){
                if(res > j - i + 1){
                    left = i;
                    right = j;
                    res = j - i + 1;
                }
                c = s.charAt(i++);
                if(mapT.containsKey(c)){
                    //mapS.merge(c, -1, Integer::sum);
                    mapS.put(c, mapS.getOrDefault(c, 0) - 1);
                    if(mapS.get(c).compareTo(mapT.get(c)) < 0){
                        cnt--;
                    }
                }
            }

        }

        return res == Integer.MAX_VALUE ? "" : s.substring(left, right + 1);
    }
}
相关推荐
ghie90905 分钟前
基于MATLAB的遗传算法优化支持向量机实现
算法·支持向量机·matlab
朝新_41 分钟前
【优选算法】第一弹——双指针(上)
算法
艾莉丝努力练剑1 小时前
【C++STL :stack && queue (一) 】STL:stack与queue全解析|深入使用(附高频算法题详解)
linux·开发语言·数据结构·c++·算法
CoovallyAIHub1 小时前
ICLR 2026 惊现 SAM 3,匿名提交,实现“概念分割”,CV领域再迎颠覆性突破?
深度学习·算法·计算机视觉
IT古董1 小时前
【第五章:计算机视觉-计算机视觉在工业制造领域中的应用】1.工业缺陷分割-(2)BiseNet系列算法详解
算法·计算机视觉·制造
电鱼智能的电小鱼2 小时前
服装制造企业痛点解决方案:EFISH-SBC-RK3588 预测性维护方案
网络·人工智能·嵌入式硬件·算法·制造
yan8626592462 小时前
于 C++ 的虚函数多态 和 模板方法模式 的结合
java·开发语言·算法
小此方2 小时前
C语言自定义变量类型结构体理论:从初见到精通(下)
c语言·数据结构·算法
_poplar_3 小时前
15 【C++11 新特性】统一的列表初始化和变量类型推导
开发语言·数据结构·c++·git·算法
CoovallyAIHub3 小时前
YOLO Vision 2025 还没结束!亚洲首场登陆深圳,YOLO26有望亮相
深度学习·算法·计算机视觉