class Solution { public: vector<int> maxSlidingWindow(vector<int>& nums, int k) { vector<int> ret; if(nums.size()==0) return ret; deque<int> q; for(int i=0; i<nums.size(); ++i) { if(q.empty() || nums[q.back()] > nums[i] ) { q.push_back(i); } else { q.pop_back(); while(!q.empty() && nums[q.back()] <= nums[i] ) { q.pop_back(); } q.push_back(i); } //减数逻辑 if(i-k+1 > q.front()) { q.pop_front(); } if(i>=k-1) { ret.push_back(nums[q.front()]); } } return ret; } };
原文地址:https://www.cnblogs.com/randyniu/p/9396178.html
时间: 2024-11-02 13:10:08