20210310 & 20210311 :栈、队列、堆类题目合集

栈、队列、堆类题目合集

写在前面

  1. 栈、队列、堆基本的题目就是这些,需要特殊技巧的另算,重写一遍保证熟练掌握这些题目并且达到熟练使用的程度。记录以便后续再次学习。

题目列表

    1. 用队列实现栈
      在这里插入图片描述
    1. 用栈实现队列
      在这里插入图片描述
    1. 最小栈
      在这里插入图片描述
    1. 数组中的第K个最大元素
      在这里插入图片描述
    1. 数据流的中位数
      在这里插入图片描述

思路分析

    1. 用队列实现栈
    1. 用栈实现队列
    1. 最小栈
    1. 数组中的第K个最大元素(堆实现)
    1. 数据流的中位数(大小堆实现)
  1. 注意1-3题只是增加对栈和队列的数据结构的理解,而4-5主要学会使用堆,具体就是大小堆的理解和使用。这一点不管是用cpp还是java都是一样的。重在理解。
  2. 目前对于这两类的数据结构都掌握的更加清晰了。
  3. 继续保持!

代码实现

    1. 用队列实现栈
class MyStack {
public:
    /** Initialize your data structure here. */
    MyStack() {

    }
    
    /** Push element x onto stack. */
    void push(int x) {
        // 新队伍
        queue<int> temp_queue;
        // 新来的先排
        temp_queue.push(x);
        // 老人再跟上
        while (!init_queue.empty()) {
            temp_queue.push(init_queue.front());
            init_queue.pop();
        }
        // 最后重新排回原始队列
        while (!temp_queue.empty()) {
            init_queue.push(temp_queue.front());
            temp_queue.pop();
        }
    }
    
    /** Removes the element on top of the stack and returns that element. */
    int pop() {
        int tmp = init_queue.front();
        init_queue.pop();
        return tmp;
    }
    
    /** Get the top element. */
    int top() {
        return init_queue.front();
    }
    
    /** Returns whether the stack is empty. */
    bool empty() {
        return init_queue.empty();
    }
private:
    queue<int> init_queue;
};

/**
 * 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();
 */
    1. 用栈实现队列
class MyQueue {
public:
    /** Initialize your data structure here. */
    MyQueue() {

    }
    
    /** Push element x to the back of queue. */
    void push(int x) {
        // 临时栈
        stack<int> temp_stack;
        // 先将老人都出栈进新栈
        while (!init_stack.empty()) {
            temp_stack.push(init_stack.top());
            init_stack.pop();
        }
        // 再把新来的x入原来的栈init_stack,最后再把老人接回来
        init_stack.push(x);
        while (!temp_stack.empty()) {
            init_stack.push(temp_stack.top());
            temp_stack.pop();
        }
    }
    
    /** Removes the element from in front of queue and returns that element. */
    int pop() {
        int tmp =init_stack.top();
        init_stack.pop();
        return tmp;
    }
    
    /** Get the front element. */
    int peek() {
        return init_stack.top();
    }
    
    /** Returns whether the queue is empty. */
    bool empty() {
        return init_stack.empty();
    }
private:
    stack<int> init_stack;
};

/**
 * 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();
 */
    1. 最小栈
class MinStack {
public:
    /** initialize your data structure here. */
    MinStack() {

    }
    
    void push(int x) {
        // 将数据先压入数据栈_data,
        _data.push(x);
        if (_min.empty()) {
            _min.push(x);
        } else {
            if ( x > _min.top()) {
                x = _min.top();
            }
            _min.push(x);
        }
    }
    
    void pop() {
        _data.pop();
        _min.pop();
    }
    
    int top() {
        return _data.top();
    }
    
    int getMin() {
        return _min.top();
    }

private:
    stack<int> _min;
    stack<int> _data;
};

/**
 * Your MinStack object will be instantiated and called as such:
 * MinStack* obj = new MinStack();
 * obj->push(x);
 * obj->pop();
 * int param_3 = obj->top();
 * int param_4 = obj->getMin();
 */
    1. 数组中的第K个最大元素
class Solution {
public:
    int findKthLargest(vector<int>& nums, int k) {
        priority_queue<int,vector<int>,greater<int>> p_queue;
        for (int i = 0; i < nums.size(); ++i) {
            if (p_queue.size() < k) {
               p_queue.push(nums[i]); 
            } else if (p_queue.top() < nums[i]) {
                p_queue.pop();
                p_queue.push(nums[i]);
            }
        }
        return p_queue.top();
    }
};
    1. 数据流的中位数
class MedianFinder {
    priority_queue<int> lo;                              // max heap
    priority_queue<int, vector<int>, greater<int>> hi;   // min heap

public:
    // Adds a number into the data structure.
    void addNum(int num)
    {
        lo.push(num);                                    // Add to max heap

        hi.push(lo.top());                               // balancing step
        lo.pop();

        if (lo.size() < hi.size()) {                     // maintain size property
            lo.push(hi.top());
            hi.pop();
        }
    }

    // Returns the median of current data stream
    double findMedian()
    {
        return lo.size() > hi.size() ? (double) lo.top() : (lo.top() + hi.top()) * 0.5;
    }
};


/**
 * Your MedianFinder object will be instantiated and called as such:
 * MedianFinder* obj = new MedianFinder();
 * obj->addNum(num);
 * double param_2 = obj->findMedian();
 */
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

IMMUNIZE

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

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

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

打赏作者

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

抵扣说明:

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

余额充值