当前位置:网站首页>[jzof] 09 realize queue with two stacks
[jzof] 09 realize queue with two stacks
2022-07-23 13:19:00 【Sighed, angry】
The idea is simple :
Using the first in first out rule simulation of stack to realize the first in first out of queue
1、 When inserting , Directly inserted into the stack1
2、 When it pops up , When stack2 Not empty , eject stack2 Top element of stack , If stack2 It's empty , take stack1 All the numbers in the stack are stacked one by one stack2, Then pop up stack2 Top element of stack .
import java.util.Stack;
public class Solution {
Stack<Integer> stack1 = new Stack<Integer>();
Stack<Integer> stack2 = new Stack<Integer>();
public void push(int node) {
stack1.push(node);
}
public int pop() {
if (!stack2.isEmpty()) {
return stack2.pop();
} else{
while (!stack1.isEmpty()) {
stack2.push(stack1.pop());
}
}
return stack2.pop();
}
}
边栏推荐
- 方法区、永久代、元空间的关系
- OpenCV 视频操作
- EasyGBS平臺出現錄像無法播放並存在RTMP重複推流現象,是什麼原因?
- What is the reason for the failure of video playback and RTMP repeated streaming on easygbs platform?
- 当输入网址后,到网页显示,期间发生了什么
- Record a reptile question bank
- 基于redis+lua进行限流
- 力扣 剑指 Offer II 094. 最少回文分割
- [actf2020 freshman competition]backupfile 1
- Is it safe to open an account with Guosen Securities software? Will the information be leaked?
猜你喜欢

【JZOF】12矩阵中的路径

CORTEX-A系列处理器

Intégrité du signal (si) intégrité de l'alimentation électrique (PI) notes d'apprentissage (32) Réseau de distribution d'énergie (4)

C语言-大端存储和小端存储

北汇信息12岁啦|Happy Birthday

Static routing principle and configuration

CAN控制器的位同步过程

Opencv image processing (Part 1): geometric transformation + morphological operation

The context of virtual memory technology (Part 1)

OpenCV图像处理(中) 图像平滑+直方图
随机推荐
Paging collections using streams
Image processing image feature extraction and description
第十一天笔记
成功 万象奥科与CODESYS技术联合调测
信号完整性(SI)电源完整性(PI)学习笔记(三十一)电源分配网路(三)
Confused, work without motivation? Career development hopeless? It's enough to read this article
倍福PLC和C#通过ADS通信传输int类型变量
Beifu PLC and C transmit bool type variables through ads communication
信号完整性(SI)电源完整性(PI)学习笔记(三十二)电源分配网路(四)
Harbor deployment
Numpy: quick start to basic operations
倍福PLC和C#通过ADS通信传输String数组类型变量
EasyGBS平台出现录像无法播放并存在RTMP重复推流现象,是什么原因?
根据不同时间统计不同类型的数据(存储过程)
倍福PLC和C#通过ADS通信定时刷新IO
Intercept the specified range data set from list < map >
将集合使用流进行分页
Opencv image processing (Part 2): edge detection + template matching + Hough transform
Current limiting based on redis+lua
倍福PLC和C#通过ADS通信传输int数组类型变量