代码随想录算法训练营DAY10第五章 栈与队列part01

目录

[232. 用栈实现队列](#232. 用栈实现队列)

[225. 用队列实现栈](#225. 用队列实现栈)

[20. 有效的括号](#20. 有效的括号)

[1047. 删除字符串中的所有相邻重复项](#1047. 删除字符串中的所有相邻重复项)


232. 用栈实现队列

cpp 复制代码
class MyQueue {
public:
    stack<int> in;
    stack<int> out;
    MyQueue() {
    }
    void push(int x) {
        in.push(x);
    }
    int pop() {
        if(out.empty()){
            while(!in.empty()){
                out.push(in.top());
                in.pop();
            }
        }
        int res=out.top();
        out.pop();
        return res;
    }
    int peek() {
        int res=this->pop();
        out.push(res);
        return res;
    }
    bool empty() {
        return in.empty()&&out.empty();
    }
};

/**
 * Your MyQueue object will be instantiated and called as such:
 * MyQueue* obj = new MyQueue();
 * obj->push(x);
 * int param_2 = obj->pop();
 * int param_3 = obj->peek();
 * bool param_4 = obj->empty();
 */

225. 用队列实现栈

cpp 复制代码
class MyStack {
public:
    queue<int> q1;
    queue<int> q2;
    MyStack() {
    }
    void push(int x) {
        q1.push(x);   
    }
    int pop() {
        int size=q1.size();
        size--;
        while(size--){
            q2.push(q1.front());
            q1.pop();
        }
        int res=q1.front();
        q1.pop();
        q1=q2;
        while(!q2.empty()){
            q2.pop();
        }   
        return res;
    }
    int top() {
        int size=q1.size();
        size--;
        while(size--){
            q2.push(q1.front());
            q1.pop();
        }
        int res=q1.front();
        q2.push(q1.front());
        q1.pop();
        q1=q2;
        while(!q2.empty()){
            q2.pop();
        }   
        return res;
    }
    bool empty() {
        return q1.empty();   
    }
};

/**
 * Your MyStack object will be instantiated and called as such:
 * MyStack* obj = new MyStack();
 * obj->push(x);
 * int param_2 = obj->pop();
 * int param_3 = obj->top();
 * bool param_4 = obj->empty();
 */

20. 有效的括号

法一:

cpp 复制代码
class Solution {
public:
    bool isValid(string s) {
        stack<int> st;
        for (char c : s) {
            if (c == '(') {
                st.push(1);
            } else if (c == '[') {
                st.push(2);
            } else if (c == '{') {
                st.push(3);
            } else if (c == ')') {
                if (!st.empty() && st.top() == 1) {
                    st.pop();
                } else {
                    return false;
                }
            } else if (c == ']') {
                if (!st.empty() && st.top() == 2) {
                    st.pop();
                } else {
                    return false;
                }
            } else {
                if (!st.empty() && st.top() == 3) {
                    st.pop();
                } else {
                    return false;
                }
            }
        }
        if (st.empty()) {
            return true;
        }
        return false;
    }
};

法二:

cpp 复制代码
class Solution {
public:
    bool isValid(string s) {
        unordered_map<char,char>map{{')','('},{']','['},{'}','{'}};
        stack<int> st;
        for(char c:s){
            if(map.count(c)){
                if(st.empty()||st.top()!=map[c])return false;
                st.pop();
            }
            else st.push(c);
        }
        return st.empty();
    }
};

1047. 删除字符串中的所有相邻重复项

cpp 复制代码
class Solution {
public:
    string removeDuplicates(string s) {
        stack<char> st;
        for(char c:s){
            if(st.empty()||st.top()!=c){
                st.push(c);
            }
            else {
                st.pop();
            }
        }
        string res;
        while(!st.empty()){
            res+=st.top();
            st.pop();
        }
        reverse(res.begin(),res.end());
        return res;
    }
};

优化:用string充当stack

cpp 复制代码
class Solution {
public:
    string removeDuplicates(string s) {
        string res;
        for(char c:s){
            if(res.empty()||res[res.size()-1]!=c){
                res+=c;
            }
            else {
                res.pop_back();
            }
        }
        return res;
    }
};
相关推荐
Moment7 分钟前
OpenClaw 从能聊到能干差的是这 50 个 Skills 😍😍😍
前端·后端·开源
小霖家的混江龙16 分钟前
从 0 到 1 实现一个 useState
前端·javascript·react.js
晓得迷路了20 分钟前
栗子前端技术周刊第 118 期 - Oxfmt Beta、Angular GitHub stars、React 基金会...
前端·javascript·react.js
亿元程序员35 分钟前
小伙伴说我的拼图游戏用Mask不能合批...
前端
恋猫de小郭36 分钟前
AI 正在造就你的「认知卸载」,但是时代如此
前端·人工智能·ai编程
Seven9744 分钟前
虚拟线程深度解析:轻量并发编程的未来趋势
java
摸鱼的春哥1 小时前
Agent教程14:记忆才是Agent开发的核心
前端·javascript·后端
明月_清风1 小时前
Clipboard API 深度实战:如何同时存入“纯文本”和“富文本”两种格式?
前端·javascript
明月_清风1 小时前
权限陷阱:为什么你的“点击复制”在某些浏览器或 iframe 里会失效?
前端·javascript
雨中飘荡的记忆11 小时前
ElasticJob分布式调度从入门到实战
java·后端