239. [LeetCode ]Sliding Window Maximum
发布时间:2020-12-14 02:31:23 所属栏目:Windows 来源:网络整理
导读:Given an array? nums ,there is a sliding window of size? k ?which is moving from the very left of the array to the very right. You can only see the? k ?numbers in the window. Each time the sliding window moves right by one position. Return
Given an array?nums,there is a sliding window of size?k?which is moving from the very left of the array to the very right. You can only see the?k?numbers in the window. Each time the sliding window moves right by one position. Return the max sliding window. Example: Input: nums =,and k = 3 Output: Window position Max --------------- ----- [1 3 -1] -3 5 3 6 7 3 1 [3 -1 -3] 5 3 6 7 3 1 3 [-1 -3 5] 3 6 7 5 1 3 -1 [-3 5 3] 6 7 5 1 3 -1 -3 [5 3 6] 7 6 1 3 -1 -3 5 [3 6 7] 7 #include <cstdio> #include <vector> #include<deque> #include<iostream> using namespace std; class Solution { public: vector<int> maxSlidingWindow(vector<int>& nums,int k) { deque<int> buffer; vector<int> res; for(int i=0; i<nums.size();++i) { while(!buffer.empty() && nums[i]>=nums[buffer.back()]) buffer.pop_back(); buffer.push_back(i); if(i>=k-1) res.push_back(nums[buffer.front()]); if(buffer.front()<= i-k + 1) buffer.pop_front(); } return res; } }; int main(){ Solution test; vector<int> aa; aa.push_back(19); aa.push_back(-5); aa.push_back(2); aa.push_back(7); aa.push_back(6); aa.push_back(7); aa.push_back(5); aa.push_back(9); test.maxSlidingWindow(aa,3); return 0; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
推荐文章
站长推荐
- .net – 如何导出CIL DLL中的方法,以便本地程序可
- windows-server-2012 – 重新安装(修复)Server 2
- active-directory – Active Directory信任问题
- windows-server-2008 – 单个网络上的两个不同域
- windows-server-2008-r2 – 如何在重命名审核日志
- system.speech.recognition – 锁定计算机会禁用
- windows-server-2008 – 如何在Windows Server 2
- windows-phone-7 – 强制Windows Phone使用WIFI连
- windows – 如何打开新的cmd窗口并在其中执行for
- 导航 – 如何在Windows Phone 7中弹出/完成视图
热点阅读