工作室有专门的网站/企业推广网络营销
用栈实现队列
leetcode题目
请你仅使用两个栈实现先入先出队列。队列应当支持一般队列的支持的所有操作(push、pop、peek、empty):
实现 MyQueue 类:
void push(int x) 将元素 x 推到队列的末尾
int pop() 从队列的开头移除并返回元素
int peek() 返回队列开头的元素
boolean empty() 如果队列为空,返回 true ;否则,返回 false
说明:
你只能使用标准的栈操作 —— 也就是只有 push to top, peek/pop from top, size, 和 is empty 操作是合法的。
你所使用的语言也许不支持栈。你可以使用 list 或者 deque(双端队列)来模拟一个栈,只要是标准的栈操作即可。
进阶:
你能否实现每个操作均摊时间复杂度为 O(1) 的队列?换句话说,执行 n 个操作的总时间复杂度为 O(n) ,即使其中一个操作可能花费较长时间。
实现思路
维护两个栈,一个输入栈,一个输出栈。
入队列操作:向输入栈中插入;
出队列操作:如果输出栈中有元素,则弹出一个;如果输出栈中没有元素, 则将输入栈中的元素依次弹出并压入输出栈,最终将最后一个元素删除(不再压入输入栈)。
查看操作:如果输出栈中有元素,则返回输出栈顶元素;如果输出栈中没有元素,则将输入栈中的元素依次弹出并压入输出栈,最终返回最后一个元素。
代码实例
class MyQueue {
private:stack<int> input_st;stack<int> output_st;
public:/** Initialize your data structure here. */MyQueue() {}/** Push element x to the back of queue. */void push(int x) {input_st.push(x);}/** Removes the element from in front of queue and returns that element. */int pop() {int val;if (!output_st.empty()) {val = output_st.top();output_st.pop();return val;}while (!input_st.empty()) {val = input_st.top();input_st.pop();if (input_st.empty()){break;}output_st.push(val);}return val;}/** Get the front element. */int peek() {if (!output_st.empty()) {return output_st.top();}while (!input_st.empty()) {int val = input_st.top();input_st.pop();output_st.push(val);}return output_st.top();}/** Returns whether the queue is empty. */bool empty() {return output_st.empty() && input_st.empty();}
};
用队列实现栈
leetcode 题目:
https://leetcode-cn.com/problems/implement-stack-using-queues/
请你仅使用两个队列实现一个后入先出(LIFO)的栈,并支持普通队列的全部四种操作(push、top、pop 和 empty)。
实现 MyStack 类:
void push(int x) 将元素 x 压入栈顶。
int pop() 移除并返回栈顶元素。
int top() 返回栈顶元素。
boolean empty() 如果栈是空的,返回 true ;否则,返回 false 。
注意:
你只能使用队列的基本操作 —— 也就是 push to back、peek/pop from front、size 和 is empty 这些操作。
你所使用的语言也许不支持队列。 你可以使用 list (列表)或者 deque(双端队列)来模拟一个队列 , 只要是标准的队列操作即可。
实现思路
维护两个队列,随时保持一个队列为空。
插入操作:向非空队列中插入元素。
删除操作:把非空队列中的元素依次插入到空队列中,最终将最后一个元素删除,并返回。
查看栈顶元素:把非空队列中的元素依次插入到空队列中,包括最后一个元素,同时将最后一个元素返回。
代码实例
class MyStack {
private:queue<int> q1;queue<int> q2;
public:/** Initialize your data structure here. */MyStack() {}/** Push element x onto stack. */void push(int x) {//如果两个队列都为空,在q1中插入if (q1.empty() && q2.empty()) {q1.push(x);return;}if (q1.empty()) {q2.push(x);return;}if (q2.empty()) {q1.push(x);return;} }/** Removes the element on top of the stack and returns that element. */int pop() {if(q1.empty() && q2.empty()) {throw q1.empty() && q2.empty();return 0;}// 如果队列1不为空,则将里面的元素依次加入q2,将最后一个元素输出。int val ; if(q1.empty()) {while(q2.size() > 1) {val = q2.front();q2.pop();q1.push(val);}val = q2.front();q2.pop();}else if(q2.empty()) {while(q1.size() > 1) {val = q1.front();q1.pop();q2.push(val);}val = q1.front();q1.pop();}return val;}/** Get the top element. */int top() {if(q1.empty() && q2.empty()) {throw q1.empty() && q2.empty();}// 如果队列1不为空,则将里面的元素依次加入q2,将最后一个元素输出。int last = 0;if(q1.empty()) { while(q2.size() >= 1) {last = q2.front();q2.pop();q1.push(last);}}else if(q2.empty()) {while(q1.size() >= 1) {last = q1.front();q1.pop();q2.push(last);}}return last;}/** Returns whether the stack is empty. */bool empty() {return q1.empty() && q2.empty();}
};