数据结构与算法——栈、队列、堆汇总整理

例1:使用队列实现栈(easy 栈、队列)

在这里插入图片描述
在这里插入图片描述

class MyStack {
public:
    MyStack() {
    }
    void push(int x) {
        std::queue<int> temp_queue;
        temp_queue.push(x);
        while (!_data.empty()) {
            temp_queue.push(_data.front());
            _data.pop();
        }
        while (!temp_queue.empty()) {
            _data.push(temp_queue.front());
            temp_queue.pop();
        }
    }
    //下面的操作,栈和队列一致
    int pop() {
        int x = _data.front();
        _data.pop();
        return x;
    }
    int top() {
        return _data.front();
    }
    bool empty() {
        return _data.empty();
    }
private:
    std::queue<int>_data;
};

例2:使用栈实现队列(easy 栈、队列)

在这里插入图片描述

class MyQueue {
public:
    MyQueue() {
    }
    //双栈
    void push(int x) {
        input_stack.push(x);
    }
    int pop() {
        adjust();
        int x = output_stack.top();
        output_stack.pop();
        return x;
    }
    int peek() {
        adjust();
        return output_stack.top();
    }
    bool empty() {
        return (input_stack.empty() && output_stack.empty());
    }
private:
    void adjust() {
        if (!output_stack.empty()) {
            return;
        }
        while (!input_stack.empty()) {
            output_stack.push(input_stack.top());
            input_stack.pop();
        }
    }
    std::stack<int> input_stack;
    std::stack<int> output_stack;
};

例3:包含min函数的栈(easy 栈)

在这里插入图片描述
用一个单独的栈来存放最小值
在这里插入图片描述

class MinStack {
public:
    MinStack() {
    }
    void push(int x) {
        _data.push(x);
        if (_min.empty()) {
            _min.push(x);
        }
        else {
            if (x > _min.top()) {
                x = _min.top();
            }
            _min.push(x);
        }
    }
    void pop() {
        _min.pop();
        _data.pop();
    }
    int top() {
        return _data.top();
    }
    int getMin() {
        return _min.top();
    }
private:
    std::stack<int> _data;
    std::stack<int> _min;
};

例4:合法的出栈序列(medium 栈、队列)

题目:在这里插入图片描述
思路:
在这里插入图片描述

bool check_is_valid_order(std::queue<int>& order) {
    std::stack<int> s;//s为模拟栈
    int n = order.size();
    for (int i = 0; i <= n; i++) {
        s.push(i);
        while (!s.empty() && s.top() == order.front()) {
            s.pop();
            order.pop();
        }
    }
    if (!s.empty()) {
        return false;
    }
    return true;
}

例5:简单的计算器(hard 栈)

力扣—— 224. 基本计算器(困难)

例6:数组中第K大的数(easy 堆)

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

class Solution {
public:
    int findKthLargest(vector<int>& nums, int k) {
        priority_queue<int, vector<int>, greater<int> > q;
        for (int i = 0; i < nums.size(); i++) {
            if (q.size() < k) {
                q.push(nums[i]);
            }
            else if (q.top() < nums[i]) {
                q.pop();
                q.push(nums[i]);
            }
        }
        return q.top();
    }
};

例7:寻找中位数(hard 堆)

力扣—— 295. 数据流的中位数(困难)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

小屋*

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

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

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

打赏作者

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

抵扣说明:

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

余额充值