栈和队列
栈和队列的使用场景和相关算法题实现。
✏️ 1、栈(Stack)& 队列(queue)
🖋️ 1.1、栈实现队列
class MyQueue {
public:
/** Initialize your data structure here. */
MyQueue() {
m_stack1 = new std::stack<int>();
m_stack2 = new std::stack<int>();
}
/** Push element x to the back of queue. */
void push(int x) {
m_stack1->push(x);
}
/** Removes the element from in front of queue and returns that element. */
int pop() {
if(m_stack2->empty()){
while(!m_stack1->empty()){
m_stack2->push(m_stack1->top());
m_stack1->pop();
}
}
int re = m_stack2->top();
m_stack2->pop();
return re;
}
/** Get the front element. */
int peek() {
if(m_stack2->empty()){
while(!m_stack1->empty()){
m_stack2->push(m_stack1->top());
m_stack1->pop();
}
}
return m_stack2->top();
}
/** Returns whether the queue is empty. */
bool empty() {
return m_stack1->empty() && m_stack2->empty();
}
private:
std::stack<int> *m_stack1;
std::stack<int> *m_stack2;
};🖋️ 1.2、队列实现栈
🖋️ 1.3、双端队列(deque: double-ended queue)
🖋️ 1.4、最小栈
🖋️ 1.5、题型
✏️ 2、单调栈和单调队列
🖋️ 2.1、单调栈(维护凸包)
🖋️ 2.2、单调队列
💎 2.2.1、特点:
💎 2.2.2、单调队列的原理:
🖋️ 2.3、单调队列和单调栈的性质
🖋️ 2.4、单调队列和单调栈的应用
单调队列
单调栈
🖋️ 2.5、题型
1、保留最大的数
4、视野总和(摩天大楼)
8、发射站
9、牛宫
✏️ 3、优先队列
🖋️ 3.1、优先队列 & 左式堆
🖋️ 3.2、题型
最后更新于