标签:
题目描述:public class MinStack { private Stack<int> _stack ; private int? _min; public MinStack(){ _stack = new Stack<int>(); } public void Push(int x) { if(!_min.HasValue || x < _min){ _min = x; } _stack.Push(x); } public void Pop() { var x = _stack.Pop(); if (x == _min){ if(_stack.Count > 0){ _min = _stack.Min(); } else{ _min = null; } } } public int Top() { return _stack.Peek(); } public int GetMin() { return _min.Value; } }
标签:
原文地址:http://blog.csdn.net/lan_liang/article/details/49962363