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

运行结果

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

相关推荐
米粒126 分钟前
力扣算法刷题 Day 31 (贪心总结)
算法·leetcode·职场和发展
少许极端30 分钟前
算法奇妙屋(四十)-贪心算法学习之路7
java·学习·算法·贪心算法
AlenTech1 小时前
647. 回文子串 - 力扣(LeetCode)
算法·leetcode·职场和发展
py有趣1 小时前
力扣热门100题之合并两个有序链表
算法·leetcode·链表
8Qi81 小时前
LeetCode热题100--45.跳跃游戏 II
java·算法·leetcode·贪心算法·编程
foundbug9992 小时前
基于STM32的步进电机加减速程序设计(梯形加减速算法)
stm32·单片机·算法
CheerWWW2 小时前
C++学习笔记——this关键字、对象生命周期(栈作用域)、智能指针、复制与拷贝构造函数
c++·笔记·学习
lucky九年2 小时前
GO语言模拟C++封装,继承,多态
开发语言·c++·golang
北顾笙9802 小时前
day12-数据结构力扣
数据结构·算法·leetcode
凌波粒2 小时前
LeetCode--454.四数相加 II(哈希表)
算法·leetcode·散列表