STL:容器—棧

☀️Stack☀️

與隊列不同,棧是一個LIFO數據結構。通常,插入操作在棧中被稱作入棧push。與隊列類似,總是在堆棧的末尾添加一個新元素。但是,刪除操作,退棧pop,將始終刪除隊列中相對於它的最後一個元素。

Stack簡介

stack是一種先進後出(First In Last Out,FILO)的數據結構,它只有一個出口,形式如圖所示。stack容器允許新增元素,移除元素,取得棧頂元素,但是除了最頂端外,沒有任何其他方法可以存取stack的其他元素。換言之,stack不允許有遍歷行爲。
有元素推入棧的操作稱爲:push,將元素推出stack的操作稱爲pop.
在這裏插入圖片描述

⭕️Stack沒有迭代器⭕️

和隊列一樣,可以看我的另一篇博客

常用方法

1、大小操作

size()  //大小
empty()  //判空

2、存取操作

push(elem)  //插入進棧頂
pop()  //彈出棧頂
top()  //訪問棧頂

3、遍歷

while(!s.empty())
{
	cout << s.top() << endl;
	s.pop();
}

4、綜合練習

☁️是時候展示真正的力量了!奧利給!👍
💻這個代碼必須在力扣上面運行,否則要加頭文件

#include <iostream>

class MyStack {
    private:
        vector<int> data;               // store elements
    public:
        /** Insert an element into the stack. */
        void push(int x) {
            data.push_back(x);
        }
        /** Checks whether the queue is empty or not. */
        bool isEmpty() {
            return data.empty();
        }
        /** Get the top item from the queue. */
        int top() {
            return data.back();
        }
        /** Delete an element from the queue. Return true if the operation is successful. */
        bool pop() {
            if (isEmpty()) {
                return false;
            }
            data.pop_back();
            return true;
        }
};

int main() {
    MyStack s;
    s.push(1);
    s.push(2);
    s.push(3);
    for (int i = 0; i < 4; ++i) {
        if (!s.isEmpty()) {
            cout << s.top() << endl;
        }
        cout << (s.pop() ? "true" : "false") << endl;
    }
}

是不是對棧有了一定的瞭解啦?來做題吧!😄

Stack經典題目

📚1、判斷迴文-棧-超簡單

同樣不給代碼,超簡單,隊棧都可實現

📚2、字符串翻轉-超簡單

不給代碼,給點僞代碼,自己思考
🎍用到string字符串類,也是STL
🎍用到char類型的stack
🎍遍歷

#include <string>
#include <stack>

每次彈出賦值進入新的字符串,判斷舊字符串和新字符串是否相等

📚3、刪除最外層的括號-簡單-力扣

題目地址:https://leetcode-cn.com/tag/stack/
在這裏插入圖片描述

class Solution {
public:
    string removeOuterParentheses(string S) {
        int L = 1; int R = 0; //左右指針
        string ans; //string就是STL模板庫,可以實現模擬棧
        for(int i = 1; i < S.size(); i++){
            if(S[i] == '(')L++;
            else R++;
            if(R != L)
                ans.push_back(S[i]);
            else {
                i++;
                L = 1;
                R = 0;
            }
        }
        return ans;
    }
};

📚4、有效的括號-簡單-經典-力扣

棧的經典應用就是配對括號
題目地址:https://leetcode-cn.com/problems/valid-parentheses/
簡單!瞬間通過

class Solution {
public:
    bool isValid(string s) {
        if(s.length()%2!=0)return false;//如果長度爲奇數,false
        stack<char> mystack;
        for(char &c : s){
            if(c=='(' || c=='[' || c=='{')mystack.push(c);//壓入棧中
            else{
                if(mystack.empty())return false;
                if(c==')' && mystack.top()!='(')return false;
                if(c==']' && mystack.top()!='[')return false;
                if(c=='}' && mystack.top()!='{')return false;
                mystack.pop();
            }
        }
        return mystack.empty();
    }
};

5、更多題目

我們在LeetCode見👋

參考:

1、https://blog.csdn.net/qq_42322103/article/details/99685797
2、https://leetcode-cn.com/tag/stack/
3、https://leetcode-cn.com/explore/learn/card/queue-stack/218/stack-last-in-first-out-data-structure/875/

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章