[代码随想录10]栈和队列

前言

栈和队列在STL中扮演的什么角色呢?我们知道STL的六大组件是:容器,适配器,算法,迭代器,空间配置器,仿函数,而我们今天要学的栈和队列就是属于适配器里面的,为什么栈和队列符合适配器呢?因为他们两的实现可以依赖于底层的数据结构容器而生的,也就是栈可以用vector或者List或者其他容器实现。按照自己的需求去选取

 

题目链接

232. 用栈实现队列 - 力扣(LeetCode) 

225. 用队列实现栈 - 力扣(LeetCode) 

20. 有效的括号 - 力扣(LeetCode) 

1047. 删除字符串中的所有相邻重复项 - 力扣(LeetCode) 

一、用栈实现队列

思路:主要就是借助辅助栈,把数据倒出来就可以实现队头入,队尾出。获取队首元素 peek() :当栈 B 不为空: B中仍有已完成倒序的元素,因此直接返回 B 的栈顶元素。否则,当 A 为空: 即两个栈都为空,无元素,因此返回 -1 。否则: 将栈 A 元素全部转移至栈 B 中,实现元素倒序,并返回栈 B 的栈顶元素。

 

private:
    std::stack<int>A,B;
public:
    MyQueue() { }
    void push(int x) {
        A.push(x);
    }
    int pop() {
        int peek=this->peek();
        B.pop();
        return peek;
    }
    int peek() {
        if(!B.empty()) return B.top();
        if(A.empty()) return -1;
        while(!A.empty()){
            B.push(A.top()),A.pop();
        }
        int res=B.top();
        return res;
    }
    bool empty() {
        return A.empty()&&B.empty();
    }

 

二、用队列实现栈

 思路:使用两个队列实现栈的操作,其中 queue1​ 用于存储栈内的元素,queue2​ 作为入栈操作的辅助队列。

也可以使用一个队列去实现,即最后入栈的元素最先出栈,同样需要满足队列前端的元素是最后入栈的元素。只需要稍微修改一下队列的逻辑。

public:
    queue<int> queue1;
    queue<int> queue2;

    MyStack() { 
    }
    void push(int x) {
        queue2.push(x);
        while(!queue1.empty()){
            queue2.push(queue1.front());
            queue1.pop();
        }
        swap(queue1,queue2);
    }
    int pop() {
        int r=queue1.front();
        queue1.pop();
        return r;
    }
    int top() {
        int r=queue1.front();
        return r;
    }
    bool empty() {
        return queue1.empty();
    }
    public:
    queue<int> q;
    MyStack(){}
    void push(int x){
        int n=q.size();
        q.push(x);
        for(int i=0;i<n;i++){
            q.push(q.front());
            q.pop();
        }
    }
    int pop(){
        int r=q.front();
        q.pop();
        return r;
    }
    int top(){
        int r=q.front();
        return r;
    }
    bool empty(){
        return q.empty();
    }

 

三、有效的括号

 思路:借助栈结构,玩对碰对,匹配则抵消,看最后是否为空。

    bool isValid(string s) {
        if(s.size()%2!=0)return false;
        stack<char> st;
        for(int i=0;i<s.size();i++){
            if(s[i]=='(')st.push(')');
            else if(s[i]=='{')st.push('}');
            else if(s[i]=='[')st.push(']');
            else if(st.empty()||st.top()!=s[i])return false;
            else st.pop();
        }
        return st.empty();
    }

四、删除字符串中的所有相邻的重复项

 思路:栈的经典题目,直接入相同则抵消。

    string removeDuplicates(string s) {
        stack<char> st;
        for (char c : s) {
            if (st.empty() || c != st.top()) {
                st.push(c);
            } else {
                st.pop(); 
            }
        }
        string result = "";
        while (!st.empty()) { 
            result += st.top();
            st.pop();
        }
        reverse (result.begin(), result.end()); 
        return result;
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值