滑动窗口leetcode 209和76

一、leetcode 209. 长度最小的子数组

代码:

cpp 复制代码
class Solution {
public:
    int minSubArrayLen(int target, vector<int>& nums) {
        int n = nums.size();
        int left = 0;
        int sum = 0;
        int res = 100001;
        for(int right = 0;right <n;right++){
            sum += nums[right];
            while(sum >= target){
                res = min(res,right-left+1);
                sum -= nums[left];
                left++;
            }
        }
        if(res == 100001)
            res = 0;
        return res;
    }
};

二、leetcode76. 最小覆盖子串

代码:

cpp 复制代码
class Solution {
public:
    string minWindow(string s, string t) {
        unordered_map<char,int> t_map;
        unordered_map<char,int> window_map;
        for(char ch:t){
            if(t_map.contains(ch)){
                t_map[ch]++;
            }else{
                t_map[ch] = 1;
            }
            window_map[ch] = 0;
        }

        int s_len = s.size();
        int left = 0;
        int ans_left = -1;
        int ans_len = INT_MAX;
        for(int right = 0; right <s_len;right++){
            if(!window_map.contains(s[right]))
                continue;
            window_map[s[right]]++;
            while(check(t_map,window_map) && left <= right){
                if(right -left+1 <ans_len){
                    ans_len = right -left +1;
                    ans_left = left;
                }
                if(t_map.contains(s[left]))
                    window_map[s[left]]--;
                left++;
            }
        }
        if(ans_left == -1) return "";
        return s.substr(ans_left,ans_len);
    }

    bool check(unordered_map<char,int>& t_map,unordered_map<char,int>& window_map){
        for(auto p:t_map){
            if(window_map[p.first] < t_map[p.first])
                return false;
        }
        return true;
    }
};
cpp 复制代码
class Solution {
public:
    string minWindow(string s, string t) {
        unordered_map<char,int> t_map;
        unordered_map<char,int> window_map;
        for(char ch:t){
            if(t_map.contains(ch))
                t_map[ch]++;
            else
                t_map[ch] = 1;
        }

        int s_len = s.size();
        int left = 0;
        int window_valid_char_cnt = 0;
        string ans;
        for(int right = 0; right <s_len;right++){
            if(++window_map[s[right]] <= t_map[s[right]])
                window_valid_char_cnt++;
            while(window_map[s[left]] > t_map[s[left]]){
                window_map[s[left]]--;
                left++;
            }
            if(window_valid_char_cnt == t.size()){
                if(ans.empty() || ans.size() > right-left+1)
                    ans = s.substr(left,right-left+1);
            }
        }
        return ans;
    }
};
相关推荐
NAGNIP4 小时前
大模型框架性能优化策略:延迟、吞吐量与成本权衡
算法
美团技术团队5 小时前
LongCat-Flash:如何使用 SGLang 部署美团 Agentic 模型
人工智能·算法
Fanxt_Ja10 小时前
【LeetCode】算法详解#15 ---环形链表II
数据结构·算法·leetcode·链表
侃侃_天下10 小时前
最终的信号类
开发语言·c++·算法
茉莉玫瑰花茶10 小时前
算法 --- 字符串
算法
博笙困了10 小时前
AcWing学习——差分
c++·算法
NAGNIP10 小时前
认识 Unsloth 框架:大模型高效微调的利器
算法
NAGNIP10 小时前
大模型微调框架之LLaMA Factory
算法
echoarts10 小时前
Rayon Rust中的数据并行库入门教程
开发语言·其他·算法·rust
Python技术极客10 小时前
一款超好用的 Python 交互式可视化工具,强烈推荐~
算法