当前位置:网站首页>Daily question 1: force deduction: 225: realize stack with queue
Daily question 1: force deduction: 225: realize stack with queue
2022-07-06 22:21:00 【Flying cats don't eat fish】
subject
** analysis : Use two queues to realize the structure of stack , We need to q1 and q2,q1 Keep consistent with the stack at all times ,q2 Used to play an auxiliary role . The only difference between the two is the difference of adding elements , The order of queue and stack should be opposite , therefore ,q2 The role of , Whenever an element is added to the stack ,q2 Add the element , And then q1 The elements in are queued out in turn , Enter again q2. Let's talk about it again q2 and q1 In exchange for , In order to make sure q1 Keep consistent with the elements in the stack at all times .**
class MyStack {
public MyStack() {
}
Queue <Integer> q1 = new LinkedList<>();
Queue <Integer> q2 = new LinkedList<>();
public void push(int x) {
q2.offer(x);
while(!q1.isEmpty()){
q2.offer(q1.poll());
}
Queue <Integer> temp;
temp = q2;
q2 = q1;
q1 = temp;
}
public int pop() {
return q1.poll();
}
public int top() {
return q1.peek();
}
public boolean empty() {
return q1.isEmpty();
}
}
/** * Your MyStack object will be instantiated and called as such: * MyStack obj = new MyStack(); * obj.push(x); * int param_2 = obj.pop(); * int param_3 = obj.top(); * boolean param_4 = obj.empty(); */
边栏推荐
- C # realizes crystal report binding data and printing 4-bar code
- What is the difference between animators and animators- What is the difference between an Animator and an Animation?
- 基於 QEMUv8 搭建 OP-TEE 開發環境
- Maximum product of three numbers in question 628 of Li Kou
- Lora sync word settings
- GNN, please deepen your network layer~
- 插入排序与希尔排序
- GPS from entry to abandonment (XVII), tropospheric delay
- MariaDb数据库管理系统的学习(一)安装示意图
- [线性代数] 1.3 n阶行列式
猜你喜欢
GPS从入门到放弃(十二)、 多普勒定速
硬件開發筆記(十): 硬件開發基本流程,制作一個USB轉RS232的模塊(九):創建CH340G/MAX232封裝庫sop-16並關聯原理圖元器件
C#实现水晶报表绑定数据并实现打印4-条形码
2020 Bioinformatics | GraphDTA: predicting drug target binding affinity with graph neural networks
【sciter】: 基于 sciter 封装通知栏组件
二叉(搜索)树的最近公共祖先 ●●
Oracle control file and log file management
基于 QEMUv8 搭建 OP-TEE 开发环境
嵌入式常用计算神器EXCEL,欢迎各位推荐技巧,以保持文档持续更新,为其他人提供便利
GPS from getting started to giving up (19), precise ephemeris (SP3 format)
随机推荐
Oracle Performance Analysis 3: introduction to tkprof
Insert sort and Hill sort
保存和检索字符串
[sciter bug] multi line hiding
基於 QEMUv8 搭建 OP-TEE 開發環境
OpenCV VideoCapture. Get() parameter details
The nearest common ancestor of binary (search) tree ●●
第3章:类的加载过程(类的生命周期)详解
插入排序与希尔排序
GPS从入门到放弃(十一)、差分GPS
Applet system update prompt, and force the applet to restart and use the new version
2020 Bioinformatics | GraphDTA: predicting drug target binding affinity with graph neural networks
hdu 4912 Paths on the tree(lca+馋)
Qt | UDP广播通信、简单使用案例
[sciter]: encapsulate the notification bar component based on sciter
Write a rotation verification code annotation gadget with aardio
硬件開發筆記(十): 硬件開發基本流程,制作一個USB轉RS232的模塊(九):創建CH340G/MAX232封裝庫sop-16並關聯原理圖元器件
重磅新闻 | Softing FG-200获得中国3C防爆认证 为客户现场测试提供安全保障
C # realizes crystal report binding data and printing 4-bar code
11、 Service introduction and port