欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C++實(shí)現(xiàn)LeetCode(155.最小棧)

 更新時(shí)間:2021年07月29日 16:54:29   作者:Grandyang  
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(155.最小棧),本篇文章通過簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下

[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.

這道最小棧跟原來的棧相比就是多了一個(gè)功能,可以返回該棧的最小值。使用兩個(gè)棧來實(shí)現(xiàn),一個(gè)棧來按順序存儲(chǔ) push 進(jìn)來的數(shù)據(jù),另一個(gè)用來存出現(xiàn)過的最小值。代碼如下:

C++ 解法一: 

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() 中,為什么不能用注釋掉那兩行的寫法,博主之前也不太明白為啥不能對(duì)兩個(gè) stack 同時(shí)調(diào)用 peek() 函數(shù)來比較,如果是這種寫法,那么不管 s1 和 s2 對(duì)棧頂元素是否相等,永遠(yuǎn)返回 false。這是為什么呢,這就要看 Java 對(duì)于peek的定義了,對(duì)于 peek() 函數(shù)的返回值并不是 int 類型,而是一個(gè) Object 類型,這是一個(gè)基本的對(duì)象類型,如果直接用雙等號(hào) == 來比較的話,肯定不會(huì)返回 true,因?yàn)槭莾蓚€(gè)不同的對(duì)象,所以一定要先將一個(gè)轉(zhuǎn)為 int 型,然后再和另一個(gè)進(jìn)行比較,這樣才能得到想要的答案,這也是 Java 和 C++ 的一個(gè)重要的不同點(diǎn)吧。

那么下面再來看另一種解法,這種解法只用到了一個(gè)棧,還需要一個(gè)整型變量 min_val 來記錄當(dāng)前最小值,初始化為整型最大值,然后如果需要進(jìn)棧的數(shù)字小于等于當(dāng)前最小值 min_val,則將 min_val 壓入棧,并且將 min_val 更新為當(dāng)前數(shù)字。在出棧操作時(shí),先將棧頂元素移出棧,再判斷該元素是否和 min_val 相等,相等的話將 min_val 更新為新棧頂元素,再將新棧頂元素移出棧即可,參見代碼如下:

C++ 解法二: 

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 解法二:

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 同步地址:

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

到此這篇關(guān)于C++實(shí)現(xiàn)LeetCode(155.最小棧)的文章就介紹到這了,更多相關(guān)C++實(shí)現(xiàn)最小棧內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論