代码随想录Day10 |用栈实现队列 用队列实现栈
1.题目链接:用栈实现队列 - 力扣
题目描述:请你仅使用两个栈实现先入先出队列。队列应当支持一般队列支持的所有操作(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(双端队列)来模拟一个栈,只要是标准的栈操作即可。
示例 1:
输入:
["MyQueue", "push", "push", "peek", "pop", "empty"]
[[], [1], [2], [], [], []]
输出:
[null, null, null, 1, 1, false]
解释:
MyQueue myQueue = new MyQueue();
myQueue.push(1); // queue is: [1]
myQueue.push(2); // queue is: [1, 2] (leftmost is front of the queue)
myQueue.peek(); // return 1
myQueue.pop(); // return 1, queue is [2]
myQueue.empty(); // return false
文章讲解/视频讲解:https://programmercarl.com/0232.%E7%94%A8%E6%A0%88%E5%AE%9E%E7%8E%B0%E9%98%9F%E5%88%97.html
解题思路:
栈:先进后出 队列:先进先出 利用两个栈即可实现队列
下面是代码示例:
class MyQueue {
Stack<Integer> stackIn;
Stack<Integer> stackOut;
public MyQueue() {
stackIn=new Stack<>(); //负责进栈
stackOut=new Stack<>(); //负责出栈
}
public void push(int x) {
stackIn.push(x);
}
public int pop() {
dumpstackIn();
return stackOut.pop();
}
public int peek() {
dumpstackIn();
return stackOut.peek();
}
public boolean empty() {
return stackIn.isEmpty()&&stackOut.isEmpty();
}
//如果stackOut为空,那么将stackIn中的元素全部放到stackOut中
private void dumpstackIn(){
//先进先出
if(!stackOut.isEmpty()) return;
while(!stackIn.isEmpty()){
stackOut.push(stackIn.pop());
}
}
}
2.题目链接:用队列实现栈 - 力扣
题目描述:请你仅使用两个队列实现一个后入先出(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(双端队列)来模拟一个队列 , 只要是标准的队列操作即可。
示例:
输入:
["MyStack", "push", "push", "top", "pop", "empty"]
[[], [1], [2], [], [], []]
输出:
[null, null, null, 2, 2, false]
解释:
MyStack myStack = new MyStack();
myStack.push(1);
myStack.push(2);
myStack.top(); // 返回 2
myStack.pop(); // 返回 2
myStack.empty(); // 返回 False
文章讲解/视频讲解:https://programmercarl.com/0225.%E7%94%A8%E9%98%9F%E5%88%97%E5%AE%9E%E7%8E%B0%E6%A0%88.html
解题思路:
1.利用两个队列来实现栈操作
定义两个队列que1和que2,先将元素放入到que2中,然后再导入到que1中,que2中每次都只存入一个元素,que1的元素,一个个导入到que2中,最后再进行交换即可用两个栈实现队列
下面是代码示例:
class MyStack {
Queue<Integer> queue1; //和栈中保持一样元素的队列
Queue<Integer> queue2; //辅助队列
public MyStack() {
queue1=new LinkedList<>();
queue2=new LinkedList<>();
}
public void push(int x) {
queue2.offer(x); //先放在辅助队列中
while(!queue1.isEmpty()){
queue2.offer(queue1.poll());
}
Queue<Integer> queueTemp;
queueTemp=queue1;
queue1=queue2;
queue2=queueTemp;
}
public int pop() {
return queue1.poll();
}
public int top() {
return queue1.peek();
}
public boolean empty() {
return queue1.isEmpty();
}
}
2.利用一个队列实现栈操作
一个队列在模拟弹出栈元素的时候,只需要将队列的头部元素(最后一个元素除外)重新添加到队尾,再弹出元素即可
下面是代码示例:
class MyStack{
Queue<Integer> queue;
public MyStack() {
queue=new LinkedList<>();
}
public void push(int x) {
queue.add(x);
}
public int pop() {
rePosition();
return queue.poll();
}
public int top() {
rePosition();
int result= queue.poll();
queue.add(result);
return result;
}
public boolean empty() {
return queue.isEmpty();
}
public void rePosition(){
int size=queue.size();
size--;
while (size-->0){
queue.add(queue.poll());
}
}
}
总结:
重新复习了一些关于栈和队列的基础知识。同时学会了如何使用栈实现队列的操作以及如何使用队列实现栈的操作。因为一开始在使用栈实现队列的操作的时候发现使用两个栈来实现比较简单,后面用队列实现栈操作的时候也想着的是使用两个队列来实现栈操作,后面发现一个队列就可以实现。不同的数据结构思考和使用的方式还是有很大的不一样的。
周末懒了点,果然还是只有工作日才能让人提起一点点干劲(虽然课有点多)今天这篇文章补上周五的打卡