Min Stack

时间:2015-04-02 21:00:58   收藏:0   阅读:104

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();
}


 

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!