滑动窗口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 小时前
求模运算符c
算法
大千AI助手6 小时前
DTW模版匹配:弹性对齐的时间序列相似度度量算法
人工智能·算法·机器学习·数据挖掘·模版匹配·dtw模版匹配
YuTaoShao8 小时前
【LeetCode 热题 100】48. 旋转图像——转置+水平翻转
java·算法·leetcode·职场和发展
生态遥感监测笔记8 小时前
GEE利用已有土地利用数据选取样本点并进行分类
人工智能·算法·机器学习·分类·数据挖掘
Tony沈哲8 小时前
macOS 上为 Compose Desktop 构建跨架构图像处理 dylib:OpenCV + libraw + libheif 实践指南
opencv·算法
刘海东刘海东9 小时前
结构型智能科技的关键可行性——信息型智能向结构型智能的转变(修改提纲)
人工智能·算法·机器学习
pumpkin845149 小时前
Rust 调用 C 函数的 FFI
c语言·算法·rust
挺菜的10 小时前
【算法刷题记录(简单题)003】统计大写字母个数(java代码实现)
java·数据结构·算法
mit6.82410 小时前
7.6 优先队列| dijkstra | hash | rust
算法