leetcode(155):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.

Example:

MinStack minStack = new MinStack();
minStack.push(-2);
minStack.push(0);
minStack.push(-3);
minStack.getMin(); –> Returns -3.
minStack.pop();
minStack.top(); –> Returns 0.
minStack.getMin(); –> Returns -2.**

【分析】

Java实现：

public class MinStack {

Stack<Integer> s,minS;

/** initialize your data structure here. */
public MinStack() {
s= new Stack<>();
minS = new Stack<>();
}

public void push(int x) {
s.push(x);
if(!minS.isEmpty() && minS.peek()<x) return ;
minS.push(x);
}

public void pop() {
int m = s.pop();
if(m==minS.peek()) minS.pop();
}

public int top() {
return s.peek();

}

public int getMin() {
return minS.peek();

}

}

LeetCode(155) Min Stack

2014-11-18 06:50:56

LeetCode刷题 (Python) | 155. Min Stack

2016-01-30 17:53:12

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

2015-08-21 06:30:33

LeetCode 155 : Min Stack (Java)

2015-12-12 14:58:09

leetcode 155 Min Stack C++

2016-08-18 19:10:55

[LeetCode BY Python]155. Min Stack

2018-02-06 18:41:31

Leetcode-标签为stack 155. Min Stack

2017-04-15 17:45:58

leetcode 155. Min Stack 最小栈的实现

2017-09-18 10:29:21

155. Min Stack [easy] (Python)

2016-07-27 17:13:13

LeetCode 155. Min Stack（最小栈）

2016-05-26 00:56:06