【容器适配器/栈队列】题解+详细备注(共7题)

本文介绍了如何使用栈和队列解决多种问题,如括号有效性检查、逆波兰表达式求值、滑动窗口最大值和高频元素查找。此外,还探讨了这些数据结构在实现其他复杂功能(如循环队列和二叉树操作)中的应用。通过实例展示了如何巧妙地运用数据结构来优化算法的效率。
摘要由CSDN通过智能技术生成

232.用栈实现队列

class MyQueue {
public:
    stack<int> stackIn;
    stack<int> stackOut;

    MyQueue() {

    }
    
    void push(int x) {
        stackIn.push(x);
    }
    // 如果输出队列为空、将输入队列数据搬移到输出队列中
    int pop() {
        if(stackOut.empty()){
            while(!stackIn.empty()){
                stackOut.push(stackIn.top());
                stackIn.pop();
            }
        }
        // pop数据
        int x = stackOut.top();
        stackOut.pop();
        return x;
    }
    // 如果输出队列为空、将输入队列数据搬移到输出队列中
    int peek() {
        if(stackOut.empty()){
            while(!stackIn.empty()){
                stackOut.push(stackIn.top());
                stackIn.pop();
            }
        }
		// 返回数据
        return stackOut.top();
    }
    
    bool empty() {
        return stackIn.empty() && stackOut.empty();
    }
};

225.用队列实现栈

class MyStack {
public:
    queue<int> q;
    MyStack() {

    }
    
    void push(int x) {
        q.push(x);
    }
    
    int pop() {
    	// 使用单个队列:从新完成数据的push
        int n = q.size() -1;
        while(n--){
            q.push(q.front());
            q.pop();
        }
        // 弹出最后一个数据
        int x = q.front();
        q.pop();
        return x;
    }
    
    int top() {
        return q.back();
    }
    
    bool empty() {
        return q.empty();
    }
};

20.有效的括号

class Solution {
public:
    bool isValid(string s) {
        stack<int> st;
		// 使用栈维护对应的括号
        for(int i{};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() || s[i] != st.top()){
                return false;
            }else{
                st.pop();
            }
        }

        return st.empty();
    }
};

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

class Solution {
public:
    string removeDuplicates(string s) {
        stack<int> st;
		// 使用栈存储不重复数据
        for(int i{};i<s.size();++i){
        	// 因为只删除相邻的两个字符,所以只需要判断一次即可
            if(!st.empty() && s[i] == st.top()){
                st.pop();
                continue;
            }

            st.push(s[i]);
        }

        string ans;
        while(!st.empty()){
            ans+=st.top();
            st.pop();
        }
        reverse(ans.begin(),ans.end());
        return ans;
    }
};

150.逆波兰表达式求值

class Solution {
public:
    int evalRPN(vector<string>& tokens) {
        stack<int> st;

        for(int i{};i<tokens.size();++i){
            if(tokens[i] == "+"
            ||tokens[i] == "-"
            ||tokens[i] == "*"
            ||tokens[i] == "/"){
                int num1 = st.top();
                st.pop();
                int num2 = st.top();
                st.pop();
                if(tokens[i] == "+") st.push(num2+num1);
                if(tokens[i] == "-") st.push(num2-num1);
                if(tokens[i] == "*") st.push(num2*num1);
                if(tokens[i] == "/") st.push(num2/num1);
            }else{
                st.push(stoi(tokens[i]));
            }
        }

        return st.top();
    }
};

239.滑动窗口最大值

class Solution {
public:
    vector<int> maxSlidingWindow(vector<int>& nums, int k) {
        int n = nums.size();
        // 利用优先队列声明窗口
        priority_queue<pair<int,int>> pq;
		// 初始化第一个窗口
        for(int i{};i<k;++i){
            pq.emplace(nums[i],i);
        }
		// 从第一个窗口里取最大值
        vector<int> ans{pq.top().first};
        for(int i = k;i<n;++i){
            pq.emplace(nums[i],i); 
            while(pq.top().second <= i-k){ // 窗口移动(这里是while)
                pq.pop(); 
            }

            ans.push_back(pq.top().first);
        }

        return ans;
    }
};

347.前K个高频元素

class mycomparison{
    public:
    bool operator()(const pair<int,int>&lhs,const pair<int,int> &rhs){
        return lhs.second > rhs.second;
    }
};

class Solution {
public:
    vector<int> topKFrequent(vector<int>& nums, int k) {
        // 获取频率
        unordered_map<int,int> mp;
        for(int i{};i<nums.size();++i){
            mp[nums[i]]++;
        }
        // 构建小顶堆
        priority_queue<pair<int, int>, vector<pair<int, int>>, mycomparison> pri_que;

        for(auto &[key,value] : mp){
            pri_que.emplace(key,value);
            if (pri_que.size() > k) { 
                pri_que.pop();
            }
        }
        // 返回答案
        vector<int> ans;
        while(!pri_que.empty()){
            ans.push_back(pri_que.top().first);
            pri_que.pop();
        }

        return ans;
    }
};

32.最长有效括号

42.接雨水

71.简化路径

84.柱状图中最大的矩形

85.最大矩形

94.二叉树的中序遍历

114.二叉树展开为链表

143.重排链表

144.二叉树的前序遍历

145.二叉树的后序遍历

155.最小栈

173.二叉搜索树迭代器

224.基本计算器

225.用队列实现栈

227.基本计算器II

232.用栈实现队列

234.回文链表

387.字符串中的第一个唯一字符

589.N叉树的前序遍历

590.N叉树的后序遍历

622.设计循环队列

641.设计循环双端队列

844.比较含退格的字符串

918.环形子数组的最大和

1249.移除无效的括号

1823.找出游戏的获胜者

2071.你可以安排的最多任务数目

2073.买票需要的时间

2030.含特定字母的最小子序列

剑指Offer09.用两个栈实现队列

剑指Offer30.包含min函数的栈

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一二三o-0-O

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值