20210310 & 20210311: A collection of stack, queue, and heap topics

A collection of stack, queue, and heap topics

Write in front

  1. These are the basic problems of stacks, queues, and heaps. Special skills are required. Rewrite them to ensure that you are proficient in these topics and reach the level of proficiency. Record so that you can learn again later.

Question list

    1. Implement stack with queue
      Insert picture description here
    1. Implement queues with stacks
      Insert picture description here
    1. Minimum stack
      Insert picture description here
    1. The Kth largest element in the array
      Insert picture description here
    1. Median of data stream
      Insert picture description here

Thinking analysis

    1. Implement stack with queue
    1. Implement queues with stacks
    1. Minimum stack
    1. The Kth largest element in the array (heap implementation)
    1. The median of the data stream (size heap implementation)
  1. Note that questions 1-3 only increase the understanding of the data structure of stacks and queues, while 4-5 mainly learn to use the heap, specifically the understanding and use of large and small heaps. This is the same whether you use cpp or java. The emphasis is on understanding.
  2. At present, we have a clearer grasp of these two types of data structures.
  3. Keep it up!

Code

    1. Implement stack with queue
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. Implement queues with stacks
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. Minimum stack
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. The Kth largest element in the array
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. Median of data stream
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();
 */

Guess you like

Origin blog.csdn.net/qq_36828395/article/details/114680578