标签:signed code span class scribe 共存 return while 代码
队列存元素:
1 class Solution { 2 public: 3 vector<int> maxInWindows(const vector<int>& num, unsigned int size) 4 { 5 if(size==0 or num.size()<size){return {};} 6 int k=size; 7 deque<int> que; 8 vector<int> res; 9 int le=0,ri=0; 10 while(ri<num.size()){ 11 while(ri-le<k){ 12 while(not que.empty() and que.back()<num[ri]){ 13 que.pop_back(); 14 } 15 que.push_back(num[ri++]);//存元素 16 } 17 res.push_back(que.front()); 18 le++; 19 if(que.front()==num[le-1]){ 20 que.pop_front(); 21 } 22 } 23 return res; 24 } 25 };
队列中保存数组索引也可以:
1 class Solution { 2 public: 3 vector<int> maxInWindows(const vector<int>& num, unsigned int size) 4 { 5 if(num.size()<size){return {};} 6 int k=size; 7 deque<int> que; 8 vector<int> res; 9 int le=0,ri=0; 10 while(ri<num.size()){ 11 while(ri-le<k){ 12 while(not que.empty() and num[que.back()]<num[ri]){ 13 que.pop_back(); 14 } 15 que.push_back(ri++);//存索引 16 } 17 res.push_back(num[que.front()]); 18 le++; 19 if(que.front()==le-1){ 20 que.pop_front(); 21 } 22 } 23 return res; 24 } 25 };
标签:signed code span class scribe 共存 return while 代码
原文地址:https://www.cnblogs.com/FdWzy/p/12309275.html