【64】滑动窗口的最大值
参与人数:2130 时间限制:1秒空间限制:32768K
题目描述 给定一个数组和滑动窗口的大小,找出所有滑动窗口里数值的最大值。 例如,如果输入数组{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]}。 牛客网题目链接:点击这里
VS2010代码:
#include<iostream>
#include<vector>
using namespace std;
class Solution {
public:
vector<int> maxInWindows(
const vector<int>& num,
unsigned int size)
{
vector<int> result;
if( num.empty() || size==
0 || size>num.size() )
return result;
for(
int i=
0; i<=num.size()-size; i++)
{
int max=num[i];
for(
int j=
1; j<size; j++)
{
if(num[i+j]>max) max=num[i+j];
}
result.push_back(max);
}
return result;
}
};
int main()
{
vector<int> test1;
test1.push_back(
2);
test1.push_back(
3);
test1.push_back(
4);
test1.push_back(
2);
test1.push_back(
6);
test1.push_back(
2);
test1.push_back(
5);
test1.push_back(
1);
Solution s1;
vector<int> test;
test=s1.maxInWindows(test1,
9);
}
牛客网通过图片:
转载请注明原文地址: https://ju.6miu.com/read-1298195.html