当前位置:网站首页>每日一题:力扣:225:用队列实现栈
每日一题:力扣:225:用队列实现栈
2022-07-06 14:23:00 【会飞的猫不吃鱼】
题目

** 解析:用两个队列实现栈的结构,我们需要q1和q2,q1时刻与栈保持一致,q2用来起到一个辅助的作用。两者唯一的区别就是添加元素的区别,队列和栈的顺序应该是相反的,因此,q2的作用就显示出来了,每当栈添加一个元素的时候,q2添加该元素,然后将q1中的元素依次出队列,再进入q2。再讲q2和q1交换,以保证q1时刻与栈中元素保持一致。**
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(); */
边栏推荐
- Management background --2 Classification list
- Notes de développement du matériel (10): flux de base du développement du matériel, fabrication d'un module USB à RS232 (9): création de la Bibliothèque d'emballage ch340g / max232 SOP - 16 et Associa
- China 1,4-cyclohexanedimethanol (CHDM) industry research and investment decision-making report (2022 Edition)
- Make menuconfig has a recipe for target 'menuconfig' failed error
- Powerful domestic API management tool
- 2020 Bioinformatics | GraphDTA: predicting drug target binding affinity with graph neural networks
- 保存和检索字符串
- [daily] win10 system setting computer never sleeps
- 中国1,4-环己烷二甲醇(CHDM)行业调研与投资决策报告(2022版)
- Realization of epoll reactor model
猜你喜欢

C#實現水晶報錶綁定數據並實現打印4-條形碼

ResNet-RS:谷歌领衔调优ResNet,性能全面超越EfficientNet系列 | 2021 arxiv

Common sense: what is "preservation" in insurance?

Wechat red envelope cover applet source code - background independent version - source code with evaluation points function

墨西哥一架飞往美国的客机起飞后遭雷击 随后安全返航

Unity3D学习笔记6——GPU实例化(1)
![[sciter]: encapsulate the notification bar component based on sciter](/img/08/a3dd409261054052291e99dd28af11.png)
[sciter]: encapsulate the notification bar component based on sciter

GPS from getting started to giving up (XV), DCB differential code deviation

图像的spatial domain 和 frequency domain 图像压缩

Seata聚合 AT、TCC、SAGA 、 XA事务模式打造一站式的分布式事务解决方案
随机推荐
MongoDB(三)——CRUD
小满网络模型&http1-http2 &浏览器缓存
2020 Bioinformatics | GraphDTA: predicting drug target binding affinity with graph neural networks
LeetCode刷题(十一)——顺序刷题51至55
2022年6月国产数据库大事记-墨天轮
GPS从入门到放弃(十八)、多路径效应
Shell product written examination related
Wechat red envelope cover applet source code - background independent version - source code with evaluation points function
BarcodeX(ActiveX打印控件) v5.3.0.80 免费版使用
Mysql相关术语
C # réalise la liaison des données du rapport Crystal et l'impression du Code à barres 4
Seata聚合 AT、TCC、SAGA 、 XA事务模式打造一站式的分布式事务解决方案
anaconda安装第三方包
Is it important to build the SEO foundation of the new website
小常识:保险中的“保全”是什么?
Data processing skills (7): MATLAB reads the data in the text file TXT with mixed digital strings
Huawei has launched attacks in many industries at the same time, and its frightening technology has made European and American enterprises tremble
Unity3D学习笔记6——GPU实例化(1)
Codeforces Round #274 (Div. 2) –A Expression
The nearest common ancestor of binary (search) tree ●●