标签:
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.
#include<stack>
#include<iostream>
#include<vector>
using namespace std;
stack<int>Oridata;
stack<int>Mindata;
void push(int x) {
Oridata.push(x);
if (Mindata.empty() || x < Mindata.top())
Mindata.push(x);
else
Mindata.push(Mindata.top());
}
void pop() {
Oridata.pop();
Mindata.pop();
}
int top() {
return Oridata.top();
}
int getMin() {
return Mindata.top();
}
标签:
原文地址:http://blog.csdn.net/li_chihang/article/details/44836099