[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 題目講解彙總(持續更新中...)

相關文章
相關標籤/搜索