[LeetCode] 155. Min Stack 最小栈

 

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

  • 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:java

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.

 

这道最小栈跟原来的栈相比就是多了一个功能,能够返回该栈的最小值。使用两个栈来实现,一个栈来按顺序存储 push 进来的数据,另外一个用来存出现过的最小值。代码以下:git

 

C++ 解法一: github

class MinStack {
public:
    MinStack() {}    
    void push(int x) {
        s1.push(x);
        if (s2.empty() || x <= s2.top()) s2.push(x);
    }    
    void pop() {
        if (s1.top() == s2.top()) s2.pop();
        s1.pop();
    }  
    int top() {
        return s1.top();
    }    
    int getMin() {
        return s2.top();
    }
    
private:
    stack<int> s1, s2;
};

 

Java 解法一:函数

public class MinStack {
    private Stack<Integer> s1 = new Stack<>();
    private Stack<Integer> s2 = new Stack<>();
    
    public MinStack() {}  
    public void push(int x) {
        s1.push(x);
        if (s2.isEmpty() || s2.peek() >= x) s2.push(x);
    }
    public void pop() {
        int x = s1.pop();
        if (s2.peek() == x) s2.pop();
    }   
    public int top() {
        return s1.peek();
    }  
    public int getMin() {
        return s2.peek();
    }
}

 

须要注意的是上面的 Java 解法中的 pop() 中,为何不能用注释掉那两行的写法,博主以前也不太明白为啥不能对两个 stack 同时调用 peek() 函数来比较,若是是这种写法,那么无论 s1 和 s2 对栈顶元素是否相等,永远返回 false。这是为何呢,这就要看 Java 对于peek的定义了,对于 peek() 函数的返回值并非 int 类型,而是一个 Object 类型,这是一个基本的对象类型,若是直接用双等号 == 来比较的话,确定不会返回 true,由于是两个不一样的对象,因此必定要先将一个转为 int 型,而后再和另外一个进行比较,这样才能获得想要的答案,这也是 Java 和 C++ 的一个重要的不一样点吧。post

那么下面再来看另外一种解法,这种解法只用到了一个栈,还须要一个整型变量 min_val 来记录当前最小值,初始化为整型最大值,而后若是须要进栈的数字小于等于当前最小值 min_val,则将 min_val 压入栈,而且将 min_val 更新为当前数字。在出栈操做时,先将栈顶元素移出栈,再判断该元素是否和 min_val 相等,相等的话将 min_val 更新为新栈顶元素,再将新栈顶元素移出栈便可,参见代码以下:ui

 

C++ 解法二: url

class MinStack {
public:
    MinStack() {
        min_val = INT_MAX;
    }  
    void push(int x) {
        if (x <= min_val) {
            st.push(min_val);
            min_val = x;
        }
        st.push(x);
    }   
    void pop() {
        int t = st.top(); st.pop();
        if (t == min_val) {
            min_val = st.top(); st.pop();
        }
    }  
    int top() {
        return st.top();
    }    
    int getMin() {
        return min_val;
    }
private: int min_val; stack<int> st; };

 

Java 解法二:spa

public class MinStack {
    private int min_val = Integer.MAX_VALUE;
    private Stack<Integer> s = new Stack<>();
    
    public MinStack() {}  
    public void push(int x) {
        if (x <= min_val) {
            s.push(min_val);
            min_val = x;
        }
        s.push(x);
    }    
    public void pop() {
        if (s.pop() == min_val) min_val = s.pop();
    }   
    public int top() {
        return s.peek();
    }    
    public int getMin() {
        return min_val;
    }
}

 

Github 同步地址:code

https://github.com/grandyang/leetcode/issues/155

 

相似题目:

Sliding Window Maximum

Max Stack

 

参考资料:

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

https://leetcode.com/problems/min-stack/discuss/49014/java-accepted-solution-using-one-stack

https://leetcode.com/problems/min-stack/discuss/49016/c-using-two-stacks-quite-short-and-easy-to-understand

 

LeetCode All in One 题目讲解汇总(持续更新中...)

相关文章
相关标签/搜索