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);
    }
};

运行结果

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

相关推荐
一个人旅程~12 小时前
如何永久关闭bitlocker并防止出现更新后被强制加密?
linux·windows·经验分享·电脑
Asa1213813 小时前
Nature Microbiology|跨微生物界菌株水平传播推断的新算法TRACS
算法
_君莫笑13 小时前
Qt+Qml前后端分离上位机软件技术方案
c++·qt·用户界面·qml
叼烟扛炮13 小时前
C++ 知识点22 函数模板
开发语言·c++·算法·函数模版
Tisfy13 小时前
LeetCode 2553.分割数组中数字的数位:模拟(maybe+翻转)——java也O(1)
java·数学·算法·leetcode·题解·模拟·取模
平行侠13 小时前
33水库抽样 - 从未知大小的流中等概率采样
数据结构·算法
吴声子夜歌13 小时前
Java——Integer与二进制算法
java·算法
Controller-Inversion13 小时前
42. 接雨水
数据结构·算法·leetcode
Controller-Inversion13 小时前
33. 搜索旋转排序数组
数据结构·算法·leetcode
￰meteor13 小时前
【移动语义与移动构造】
c++