题目链接:https://leetcode.com/problems/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.
思路:
使用两个栈,一个保存数据,一个保存创建栈过程中所有最小值。
具体过程:
每次push的时候跟minStack栈顶比较,如果比它更小,则入minStack栈,所以栈顶一定是当前最小元素。
每次pop的时候跟minStack栈顶比较,如果pop的不是栈顶元素,则pop的不是最小元素,minStack不必更新,反之要更新。
算法:
- class MinStack {
- Stack val = new Stack();
- Stack minVal = new Stack();
-
- public void push(int x) {
- val.push(x);
- if (minVal.isEmpty()) {
- minVal.push(x);
- } else {
- if ((int) minVal.peek() >= x) {
- minVal.push(x);
- }
- }
- }
-
- public void pop() {
- int tmp = (int) val.pop();
- if (tmp == (int) minVal.peek()) {
- minVal.pop();
- }
- }
-
- public int top() {
- return (int) val.peek();
- }
-
- public int getMin() {
- return (int) minVal.peek();
- }
- }