LeetCode155.最小栈
题目
代码
public class MinStack
{
private Stack<int> stack;
private Stack<int> minstack;
/** initialize your data structure here. */
public MinStack()
{
stack = new Stack<int>();
minstack = new Stack<int>();
}
public void Push(int x)
{
stack.Push(x);
if (minstack.Count() == 0 || minstack.Peek() >= x)
minstack.Push(x);
}
public void Pop()
{
if (stack.Peek() == minstack.Peek())
minstack.Pop();
stack.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();
*/