zoukankan      html  css  js  c++  java
  • 给定一个数组和滑动窗口的大小,找出所有滑动窗口里数值的最大值。例如,如果输入数组{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},

    // test14.cpp : 定义控制台应用程序的入口点。
    //

    #include "stdafx.h"
    #include<iostream>
    #include<string>
    #include<cctype>
    #include <vector>
    #include<exception>
    #include <initializer_list>
    #include<stack>
    using namespace std;
    
    class Solution {
    public:
    	vector<int> maxInWindows(const vector<int>& num, unsigned int size)
    	{
    		int max ;
    	vector<int> result;
    	int	temp = --size ;
    		unsigned int count = num.size();
    
    		for (unsigned int i = 0; i <count - size; i++)
    		{
    		
    			max = num[i];
    			for (unsigned int j = i+1; size!=0; j++)
    			{
    				--size;
    				if (max < num[j])
    					max = num[j];
    			}
    			size = temp;
    			result.push_back(max);
    		}
    		return result;
    	}
    };
    
    int main()
    {
    	
    	Solution so;
    	vector<int> vec;
    	vector<int> result;
    
    	vec = { 2,3,4,2,6,2,5,1 };
    	
    	
    	result = so.maxInWindows(vec,2);
    
    	for (auto it = result.begin(); it != result.end(); it++)
    	{
    		cout << *it<< "  ";
    	}
     
    
    	cout << endl;
    	return 0;
    }
    
    注:此程序有问题 出现问题“段错误:您的程序发生段错误,可能是数组越界,堆栈溢出(比如,递归调用层数太多)等情况引起”!找不到原因
  • 相关阅读:
    关于vue中如何实现排到他思想
    js 文件下载
    js文件上传
    webpack学习笔记
    this总结
    React中props与state
    js事件总结
    js深拷贝与浅拷贝
    JS设计模式之观察者模式
    ES5与ES6的继承
  • 原文地址:https://www.cnblogs.com/wdan2016/p/5946466.html
Copyright © 2011-2022 走看看