# LeetCode -- Min Stack

Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.

push(x) -- Push element x onto stack.
pop() -- Removes the element on top of the stack.
top() -- Get the top element.
getMin() -- Retrieve the minimum element in the stack.

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;
}
}

• 本文已收录于以下专栏：

## Leetcode-155. Min Stack

• mcf171
• 2016年11月03日 18:01
• 120

## leetcode--Min Stack

leetcode--Min Stack C++/Java

## LeetCode OJ 155. Min Stack

LeetCode OJ 155. Min StackDescriptionDesign a stack that supports push, pop, top, and retrieving the...

## 【Leetcode】Min Stack

Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. ...

## 【LeetCode-面试算法经典-Java实现】【155-Min Stack（最小栈）】

【155-Min Stack（最小栈）】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】原题　　Design a stack that supports push, pop, to...

## 【LeetCode】Min Stack 解题报告

【题目】 Design a stack that supports push, pop, top, and retrieving the minimum element in constant t...
• ljiabin
• 2014年11月10日 16:17
• 17586

## [C++]LeetCode: 26 Min Stack

举报原因： 您举报文章：LeetCode -- Min Stack 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)