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.
- push(x) -- Push element x onto stack.
- pop() -- Removes the element on top of the stack.
- top() -- Get the top element.
- getMin() -- Retrieve the minimum element in the stack.
#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)
        
        
        