代码随想录算法训练营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;
    }
};
相关推荐
java干货3 分钟前
为什么 “File 10“ 排在 “File 2“ 前面?解决文件名排序的终极算法:自然排序
开发语言·python·算法
DN金猿5 分钟前
接口路径正确,请求接口却提示404
java·tomcat
皮皮哎哟11 分钟前
数据结构:嵌入式常用排序与查找算法精讲
数据结构·算法·排序算法·二分查找·快速排序
程序员清洒20 分钟前
CANN模型剪枝:从敏感度感知到硬件稀疏加速的全链路压缩实战
算法·机器学习·剪枝
vortex534 分钟前
几种 dump hash 方式对比分析
算法·哈希算法
Maynor9961 小时前
OpenClaw 玩家必备:用 AI 自动追踪社区最新动态
java·服务器·人工智能
堕2741 小时前
java数据结构当中的《排序》(一 )
java·数据结构·排序算法
亓才孓1 小时前
[Class的应用]获取类的信息
java·开发语言
开开心心就好1 小时前
AI人声伴奏分离工具,离线提取伴奏K歌用
java·linux·开发语言·网络·人工智能·电脑·blender
爱喝白开水a1 小时前
前端AI自动化测试:brower-use调研让大模型帮你做网页交互与测试
前端·人工智能·大模型·prompt·交互·agent·rag