Leetcode—76. 最小覆盖子串【困难】

2024每日刷题(167)

Leetcode---76. 最小覆盖子串

C++实现代码

cpp 复制代码
class Solution {
public:
    string minWindow(string s, string t) {
        int bestL = -1;
        int l = 0, r = 0;
        vector<int> cnt(128);
        for(const char c: t) {
            cnt[c]++;
        }
        int require = t.length();

        int minLen = s.length() + 1;
        for(; r < s.length(); r++) {
            if(--cnt[s[r]] >= 0) {
                --require;
            }
            while(require == 0) {
                if(r - l + 1 < minLen) {
                    bestL = l;
                    minLen = r - l + 1;
                }
                if(++cnt[s[l++]] > 0) {
                    ++require;
                }
            }
        }
        return bestL == -1 ? "": s.substr(bestL, minLen);
    }
};

运行结果

之后我会持续更新,如果喜欢我的文章,请记得一键三连哦,点赞关注收藏,你的每一个赞每一份关注每一次收藏都将是我前进路上的无限动力 !!!↖(▔▽▔)↗感谢支持!

相关推荐
橙几21 分钟前
击败了90%的解法?Two Sum 从 O(n²) 到 O(n) 的优化之路
算法
叶子爱分享35 分钟前
经典排序算法之归并排序(Merge Sort)
算法·排序算法
珹洺41 分钟前
C++算法竞赛篇:DevC++ 如何进行debug调试
java·c++·算法
Norvyn_71 小时前
LeetCode|Day18|20. 有效的括号|Python刷题笔记
笔记·python·leetcode
呆呆的小鳄鱼1 小时前
leetcode:冗余连接 II[并查集检查环][节点入度]
算法·leetcode·职场和发展
墨染点香1 小时前
LeetCode Hot100【6. Z 字形变换】
java·算法·leetcode
沧澜sincerely1 小时前
排序【各种题型+对应LeetCode习题练习】
算法·leetcode·排序算法
CQ_07121 小时前
自学力扣:最长连续序列
数据结构·算法·leetcode
弥彦_2 小时前
cf1925B&C
数据结构·算法
YuTaoShao2 小时前
【LeetCode 热题 100】994. 腐烂的橘子——BFS
java·linux·算法·leetcode·宽度优先