题目介绍
代码及其实验结果
public class MinStack {
Stack<int> stack=new Stack<int>();
Stack<int> minStack=new Stack<int>();
/** initialize your data structure here. */
public MinStack() {
}
public void Push(int x) {
if (minStack.Count == 0)
{
minStack.Push(x);
}
else
{
if (minStack.Peek()>= x)
{
minStack.Push(x);
}
}
stack.Push(x);
}
public void Pop() {
if (stack.Count == 0)
return;
var value = stack.Pop();
if (value == minStack.Peek())
{
minStack.Pop();
}
}
public int Top() {
return stack.Peek();
}
public int GetMin() {
return minStack.Peek();
}
}
/**
* Your MinStack object will be instantiated and called as such:
* MinStack obj = new MinStack();
* obj.Push(x);
* obj.Pop();
* int param_3 = obj.Top();
* int param_4 = obj.GetMin();
*/