//
//
// 奇数在前,偶数在后,
#include <iostream>
#include <assert.h>
#include <algorithm>
#include <stack>
#include <vector>
using namespace std;
int main()
{
int an[]={4,2,5,7,12,6,12,3,21};
stack<int,vector<int> >sta1;
stack<int,vector<int> >sta2;
int i;
// 更小空间的栈
cout<<"push:"<<endl;
for (i=0; i<sizeof(an)/sizeof(int); i++)
{
sta1.push(an[i]);
if (sta2.empty())
{
sta2.push(an[i]);
}
if (an[i]>=sta2.top())
{
sta2.push(an[i]);
}
cout<<"TopA: "<<sta1.top()<<" SizeA: "<<sta1.size()<<" TopB: "<<sta2.top()<<" SizeB: "<<sta2.size()<<endl;
}
cout<<endl<<endl<<"pop: "<<endl;
while (!sta1.empty())
{
cout<<"TopA: "<<sta1.top()<<" TopB
常数时间内求栈的最大值
最新推荐文章于 2023-10-09 16:59:09 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)