当前位置:网站首页>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(); */

边栏推荐
- Node——Egg 实现上传文件接口
- Is it safe and reliable to open an account in Caixue school and make new debts?
- Operate database transactions with jpatractionmanager
- UVM tutorial
- Use es to realize epidemic map or take out order function (including code and data)
- 智能运维实战:银行业务流程及单笔交易追踪
- [cascade classifier training parameters] training Haar cascades
- Heketi record
- Is it safe for qiniu college to open an account? How to open an account?
- 【QT】对于Qt MSVC 2017无法编译的问题解决
猜你喜欢

Barbie q! How to analyze the new game app?

关联性——组内相关系数

Windows installation WSL (II)

Talents come from afar, and Wangcheng district has consolidated the intellectual base of "strengthening the provincial capital"

4. Object mapping Mapstercover

Mysql database driver (JDBC Driver) jar package download

【QT】QtCreator卸载与安装(非正常状态)

Openvino model performance evaluation tool DL workbench

Correlation - intra group correlation coefficient

基于全志H3的QT5.12.9移植教程
随机推荐
Mysql database driver (JDBC Driver) jar package download
What is ThreadLocal memory leak and how to solve it
Relatively easy to understand PID understanding
RPA tutorial 01: Excel automation from introduction to practice
The new version of graphic network PDF will be released soon
回顾数据脱敏系统
Timer和ScheduledThreadPoolExecutor的区别
Regular expression collection
Node - generate wechat permission verification configuration
heketi 记录
2023款雷克萨斯ES产品公布,这回进步很有感
Jielizhi, production line assembly link [chapter]
Ldr6035 smart Bluetooth audio can be charged and released (5.9.12.15.20v) fast charging and fast releasing device charging
vue 强制清理浏览器缓存
Is it safe for qiniu college to open an account? How to open an account?
JS -- image to base code, base to file object
【QT】Qt 使用MSVC2017找不到编译器的解决办法
Take the enclave Park as a sample to see how Yuhua and Shaoshan play the song of Chang Zhu Tan integrated development
Using multithreaded callable to query Oracle Database
【QT】對於Qt MSVC 2017無法編譯的問題解决