当前位置:网站首页>Leetcode skimming: stack and queue 01 (realizing queue with stack)
Leetcode skimming: stack and queue 01 (realizing queue with stack)
2022-07-02 00:26:00 【Taotao can't learn English】
Use the stack to implement the following operations of the queue :
push(x) – Put an element at the end of the queue .
pop() – Remove elements from the head of the queue .
peek() – Return the elements of the queue header .
empty() – Whether the return queue is empty .
Example :
MyQueue queue = new MyQueue();
queue.push(1);
queue.push(2);
queue.peek(); // return 1
queue.pop(); // return 1
queue.empty(); // return false
explain :
- You can only use standard stack operations – It's just push to top, peek/pop from top, size, and is empty Operation is legal .
- The language you use may not support stacks . You can use list perhaps deque( deque ) To simulate a stack , As long as it's a standard stack operation .
- Suppose all operations are valid ( for example , An empty queue will not call pop perhaps peek operation ).
Ideas : Just two stacks , One is used to receive data , One is used to output data , advanced Later Backward First out .
Directly enter the stack of data Connect data push 了 .
Getting out of the stack is similar to checking the stack , First, see whether the stack of data is empty , If it's empty , Push the stack of incoming data to the stack of outgoing data , advanced +( Later + Backward )+ First out = advanced First out
It's better to judge the space , Look at the two stacks. If they are empty, they are empty .
package com.programmercarl.stacks_queues;
import java.util.Stack;
/** * @ClassName MyQueue * @Descriotion TODO * @Author nitaotao * @Date 2022/6/29 10:57 * @Version 1.0 * 232. Using stack to realize queue * https://leetcode.cn/problems/implement-queue-using-stacks/ * Ideas : * Stack , First in, then out * queue , fifo * Stack simulation queue , An element is pushed from the first stack , Push the second stack after coming out , be advanced Later Backward First out == advanced First out **/
public class MyQueue {
private Stack stackIn;
private Stack stackOut;
public MyQueue() {
stackIn = new Stack();
stackOut = new Stack();
}
/** * Push an element * * @param x */
public void push(int x) {
stackIn.push(x);
}
/** * Back to top of stack element * * @return */
public int pop() {
int lenIn = stackIn.size();
int lenOut = stackOut.size();
// The old area comes out first , New Area re-entry
if (lenOut > 0) {
return (int) stackOut.pop();
} else {
// There are no elements in the old area , The new area enters the old area
while (lenIn > 0) {
stackOut.push(stackIn.pop());
lenIn--;
}
return (int) stackOut.pop();
}
}
/** * Look at the top of the stack elements * * @return */
public int peek() {
int lenIn = stackIn.size();
int lenOut = stackOut.size();
// The old area comes out first , New Area re-entry
if (lenOut > 0) {
return (int) stackOut.peek();
} else {
// There are no elements in the old area , The new area enters the old area
while (lenIn > 0) {
stackOut.push(stackIn.pop());
lenIn--;
}
return (int) stackOut.peek();
}
}
/** * Sentenced to empty * * @return */
public boolean empty() {
return stackIn.isEmpty() && stackOut.isEmpty();
}
}
/** * 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(); * boolean param_4 = obj.empty(); */

边栏推荐
猜你喜欢
![[Qt] résoudre le problème que Qt msvc 2017 ne peut pas Compiler](/img/35/e458fd437a0bed4bace2d6d65c9ec8.png)
[Qt] résoudre le problème que Qt msvc 2017 ne peut pas Compiler

Learn online case practice

UDS bootloader of s32kxxx bootloader

【CMake】Qt creator 里面的 cmake 配置

Openvino model performance evaluation tool DL workbench
![Jielizhi, production line assembly link [chapter]](/img/1d/d1736fad33c428e61f450aad512ce0.png)
Jielizhi, production line assembly link [chapter]
![[QT] QT cannot find a solution to the compiler using msvc2017](/img/80/a4b17d6cc1ab6fe1366a3a3f43ec2e.png)
[QT] QT cannot find a solution to the compiler using msvc2017

leetcode96不同的二叉搜索樹

SQL Server 安装指南

Review data desensitization system
随机推荐
Example explanation: move graph explorer to jupyterlab
[Qt] résoudre le problème que Qt msvc 2017 ne peut pas Compiler
Selectively inhibiting learning bias for active sampling
Vue force cleaning browser cache
Selectively inhibiting learning bias for active sampling
Leetcode96 different binary search trees
Relevant settings of wechat applet cache expiration time (recommended)
Using multithreaded callable to query Oracle Database
SQL Server 安裝指南
Relatively easy to understand PID understanding
Windows10 install WSL (I) (wslregisterdistribution error)
Regular expression collection
JS -- image to base code, base to file object
JS——图片转base码 、base转File对象
leetcode96不同的二叉搜索树
The origin of usb-if Association and various interfaces
Shell process control
Mysql database driver (JDBC Driver) jar package download
Multi table operation - one to one, one to many and many to many
起床困难综合症(按位贪心)