滑动窗口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;
    }
};
相关推荐
秋说1 小时前
【PTA数据结构 | C语言版】一元多项式求导
c语言·数据结构·算法
Maybyy1 小时前
力扣61.旋转链表
算法·leetcode·链表
卡卡卡卡罗特3 小时前
每日mysql
数据结构·算法
chao_7894 小时前
二分查找篇——搜索旋转排序数组【LeetCode】一次二分查找
数据结构·python·算法·leetcode·二分查找
lifallen5 小时前
Paimon 原子提交实现
java·大数据·数据结构·数据库·后端·算法
lixzest5 小时前
C++ Lambda 表达式详解
服务器·开发语言·c++·算法
EndingCoder5 小时前
搜索算法在前端的实践
前端·算法·性能优化·状态模式·搜索算法
丶小鱼丶5 小时前
链表算法之【合并两个有序链表】
java·算法·链表
不吃洋葱.5 小时前
前缀和|差分
数据结构·算法
是白可可呀7 小时前
LeetCode 169. 多数元素
leetcode