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

边栏推荐
- 回顾数据脱敏系统
- UVM tutorial
- Dongge cashes in and the boss retires?
- JS -- image to base code, base to file object
- EMC circuit protection device for surge and impulse current protection
- Node -- add compressed file
- Relatively easy to understand PID understanding
- PWN attack and defense world cgpwn2
- Ldr6035 smart Bluetooth audio can be charged and released (5.9.12.15.20v) fast charging and fast releasing device charging
- 【模板】自适应辛普森积分
猜你喜欢

heketi 记录

实例讲解将Graph Explorer搬上JupyterLab
![Jielizhi, production line assembly link [chapter]](/img/1d/d1736fad33c428e61f450aad512ce0.png)
Jielizhi, production line assembly link [chapter]

Export default the exported object cannot be deconstructed, and module Differences between exports

Database -- sqlserver details

Graduation season is both a farewell and a new beginning

Leetcode96 different binary search trees

数据库--SqlServer详解

Selectively inhibiting learning bias for active sampling
![[QT] test whether QT can connect to the database](/img/63/32530c15995ef23bde8cadc3adfd11.png)
[QT] test whether QT can connect to the database
随机推荐
牛客-练习赛101-推理小丑
LeetCode中等题题分享(5)
Pytorch learning record
股票开户哪个证券公司比较安全
Jielizhi Bluetooth headset quality control and production skills [chapter]
ERP项目施行计划的目的是什么?
Which securities company is safer to open a stock account
What is ThreadLocal memory leak and how to solve it
【mysql 07】GPG key retrieval failed: “Couldn‘t open file /etc/pki/rpm-gpg/RPM-GPG-KEY-mysql-2022“
一个实习生的CnosDB之旅
What is the purpose of ERP project implementation plan?
heketi 记录
The difference between timer and scheduledthreadpoolexecutor
Shell process control
2023款雷克萨斯ES产品公布,这回进步很有感
[embedded system course design] a single key controls the LED light
2022 pinduoduo details / pinduoduo product details / pinduoduo SKU details
Linux CentOS7安装Oracle11g的超完美新手教程
数据库--SqlServer详解
Openvino model performance evaluation tool DL workbench