题解 | #滑动窗口的最大值#
滑动窗口的最大值
https://www.nowcoder.com/practice/1624bc35a45c42c0bc17d17fa0cba788
#include <vector> class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param num int整型vector * @param size int整型 * @return int整型vector */ int findMax(vector<int> num) { int max = num[0]; for(int i = 0;i<num.size();i++) { if(max < num[i]) max = num[i]; } return max; } vector<int> maxInWindows(vector<int>& num, int size) { // write code here vector<int> ans; if(size > num.size() || size == 0) return ans; for(int i = 0;i<num.size()-size+1;i++) { vector<int> temp(num.begin()+i,num.begin()+i+size); ans.push_back(findMax(temp)); } return ans; } };