滑动窗口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;
    }
};
相关推荐
搏博3 小时前
机器学习之五:基于解释的学习
人工智能·深度学习·学习·算法·机器学习
耀耀_很无聊5 小时前
02_使用 AES 算法实现文件加密上传至阿里云、解密下载
java·spring boot·算法·阿里云·云计算·aes·oss
江沉晚呤时7 小时前
Redis缓存穿透、缓存击穿与缓存雪崩:如何在.NET Core中解决
java·开发语言·后端·算法·spring·排序算法
achene_ql8 小时前
缓存置换:用c++实现最近最少使用(LRU)算法
开发语言·c++·算法·缓存
predisw8 小时前
垃圾收集GC的基本理解
java·jvm·算法
奔跑的乌龟_8 小时前
L3-040 人生就像一场旅行
数据结构·算法
网络骑士hrg.9 小时前
题解:洛谷 CF2091E Interesting Ratio
算法
一匹电信狗9 小时前
【数据结构】堆的完整实现
c语言·数据结构·c++·算法·leetcode·排序算法·visual studio
丝瓜蛋汤10 小时前
PCA主成分分析法(最大投影方差,最小重构距离,SVD角度)
人工智能·算法·机器学习
un_fired10 小时前
【leetcode刷题日记】lc.78-子集
算法·leetcode