leetcode155

时间:2023-01-17 03:30:36
public class MinStack
{
Stack<int> S = new Stack<int>();
/** initialize your data structure here. */ int min = int.MaxValue; public MinStack()
{ } public void Push(int x)
{
if (S.Count > )
{
if (x < min)
{
min = x;
}
}
else
{
min = x;
}
S.Push(x);
} public void Pop()
{
if (S.Count > )
{
var top = S.Pop();
if (top == min && S.Count > )
{
min = S.Min();
}
}
} public int Top()
{
return S.Peek();
} public int GetMin()
{
return min;
}
} /**
* 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();
*/

https://leetcode.com/problems/min-stack/#/description

相关文章