题目描述
给定一个数组和滑动窗口的大小,找出所有滑动窗口里数值的最大值。例如,如果输入数组{2,3,4,2,6,2,5,1}及滑动窗口的大小3,那么一共存在6个滑动窗口,他们的最大值分别为{4,4,6,6,6,5}; 针对数组{2,3,4,2,6,2,5,1}的滑动窗口有以下6个: {[2,3,4],2,6,2,5,1}, {2,[3,4,2],6,2,5,1}, {2,3,[4,2,6],2,5,1}, {2,3,4,[2,6,2],5,1}, {2,3,4,2,[6,2,5],1}, {2,3,4,2,6,[2,5,1]}。
1 class Solution { 2 public: 3 vector maxInWindows(const vector &num, unsigned int size) { 4 vector result; 5 deque Q; 6 // int n = num.size(); 7 if(num.size()num[Q.back()]) Q.pop_back();11 Q.push_back(i);12 }13 for (int i = size; i < num.size(); i++) {14 result.push_back(num[Q.front()]);15 while (!Q.empty() && num[i] >= num[Q.back()]) Q.pop_back();16 while (!Q.empty() && Q.front() <= i - size) Q.pop_front();17 Q.push_back(i);18 }19 result.push_back(num[Q.front()]);20 return result;21 }22 23 };