Leetcode239. 滑动窗口最大值-代码随想录

题目:


代码(首刷看解析 2024年1月22日):

class Solution {
private:
    class MyQueue{
    public:
        deque<int> que;
        void pop(int val){
            if (!que.empty() && que.front() == val) {
                que.pop_front();
            }
        }
        void push(int val){
            while (!que.empty() && que.back() < val) {
                que.pop_back();
            }
            que.push_back(val);
        }
        int show_max(){
            return que.front();
        }
    };
public:
    vector<int> maxSlidingWindow(vector<int>& nums, int k) {
        int n = nums.size();
        MyQueue que;
        vector<int> res;
        for (int i = 0; i < k; ++i) {
            que.push(nums[i]);
        }
        res.push_back(que.show_max());
        for (int i = 0; i < n - k; ++i) {
            que.pop(nums[i]);
            que.push(nums[i + k]);
            res.emplace_back(que.show_max());
        }
        return res;
    }
};


代码(二刷大半自解 单调递减的队列构造没弄对 2024年3月3日)

这题要注意递减队列要用deque来构建,因为push操作中对尾部元素pop(),直接用STL的queue<int>我在草稿纸上是推导出来可以的,但是代码通不过不知道为什么

class Solution {
public:
    class decreQue{
    private:
        deque<int> q;
    public:
        void pop(int val) {
            if (!q.empty() && q.front() == val) {
                q.pop_front();
            }
        }
        void push(int val) {
            while (!q.empty() && val > q.back()) {
                q.pop_back();
            }
            q.push_back(val);
        }
        int front() {
            return q.front();
        }
    }; 
    vector<int> maxSlidingWindow(vector<int>& nums, int k) {
        decreQue q1;
        for (int i = 0; i < k; ++i) {
            q1.push(nums[i]);
        }
        vector<int> res;
        res.emplace_back(q1.front());

        for (int i = 0; i < nums.size() - k; ++i) {
            q1.pop(nums[i]);
            q1.push(nums[i + k]);
            res.emplace_back(q1.front());
        }
        return res;
    }
};

代码(三刷自解 2024年5月4日 10min bugfree)

开心,第一次10min bugfree解决hard

class MyQueue{
private:
    deque<int> deq;
public:
    void push(int x) {
        while (!deq.empty() && x > deq.back()) {
            deq.pop_back();
        }
        deq.push_back(x);
    }
    void pop(int x) {
        if (x == deq.front()) {
            deq.pop_front();
        }
    }
    int show_max() {
       return deq.front(); 
    }
};
class Solution {
public:
    // 构建单调递减队列,队头->队尾 大->小
    vector<int> maxSlidingWindow(vector<int>& nums, int k) {
        MyQueue q;
        for (int i = 0; i < k; i++) {
            q.push(nums[i]);
        }
        vector<int> res;
        res.push_back(q.show_max());
        for (int i = k; i < nums.size(); i++) {
            q.pop(nums[i - k]);
            q.push(nums[i]);
            res.push_back(q.show_max());
        }
        return res;
    }
};

代码(四刷解析 2024年8月5日)

伊对一面

class Solution {
    class myQueue{
        // 单调队列类
        private:
            deque<int> q;//双向队列
        public:
            // 往队列尾部加,且新加入的数需要大于 > 尾部的数
            void push(int val) {
                while (!q.empty() && val > q.back()) {
                    q.pop_back();
                }
                q.push_back(val);
            }
            void pop(int val) { 
                if (val == q.front()) {
                    q.pop_front();
                }
            }
            int show_max() {
                return q.front();
            }
    };
public:
    vector<int> maxSlidingWindow(vector<int>& nums, int k) {
        myQueue Q;
        for (int i = 0; i < k; i++) {
            Q.push(nums[i]);
        }
        vector<int> res;
        res.push_back(Q.show_max());//注意这里先加一个
        // 遍历要从k到n
        for (int i = k; i < nums.size(); i++) {
            Q.pop(nums[i - k]);
            Q.push(nums[i]);
            res.push_back(Q.show_max());
        }
        return res;
    }
};

代码(五刷自解 2024年9月8日)

class Solution {
    class MyQueue{
    private:
        deque<int> q;
    public:
        void push(int x) {
            while (!q.empty() && x > q.back()) {
                q.pop_back();
            }
            q.push_back(x);
        }
        void pop(int x) {
            if (q.front() == x) {
                q.pop_front();
            }
        }
        int show() {
            return q.front();
        }
    };
public:
    vector<int> maxSlidingWindow(vector<int>& nums, int k) {
        MyQueue Q;
        int i = 0;
        vector<int> res;
        for (; i < k; i++) {
            Q.push(nums[i]);
        }
        res.push_back(Q.show());
        for (; i < nums.size(); i++) {
            Q.pop(nums[i - k]);
            Q.push(nums[i]);
            res.push_back(Q.show());
        }
        return res;
    }
};

代码(六刷自解 2024年9月18日)

class Solution {
public:
    class MyQueue{
        // 入队,出队,队头最大值
    private:
        deque<int> q;
    public:
        void push(int x) {
            while (!q.empty() && x > q.back()) {
                q.pop_back();
            }
            q.push_back(x);
        }

        void pop(int x) {
            if (q.front() == x) {
                q.pop_front();
            }
        }

        int top() {
            return q.front();
        }
    };
    // 单调队列:队列记录最大值
    vector<int> maxSlidingWindow(vector<int>& nums, int k) {
        MyQueue myQ;
        for (int i = 0; i < k; i++) {
            myQ.push(nums[i]);
        }
        vector<int> res;
        res.push_back(myQ.top());
        for (int i = 0; i < nums.size() - k; i++) {
            myQ.pop(nums[i]);
            myQ.push(nums[i + k]);
            res.push_back(myQ.top());
        }
        return res;
    }
};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值