题目描述
定义栈的数据结构,请在该类型中实现一个能够得到栈最小元素的min函数。
class Solution {
public:
stack<int> stack1;
stack<int> minstack;
void push(int value) {
stack1.push(value);
if(minstack.empty()||minstack.top()>value)
minstack.push(value);
else
minstack.push(minstack.top());
return;
}
void pop() {
if(!stack1.empty()&&!minstack.empty())
{
stack1.pop();
minstack.pop();
}
return;
}
int top() {
if(!stack1.empty()&&!minstack.empty())
return stack1.top();
return -1;
}
int min() {
if(!stack1.empty()&&!minstack.empty())
return minstack.top();
return -1;
}
};