当前位置:网站首页>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(); */
边栏推荐
- 重磅新闻 | Softing FG-200获得中国3C防爆认证 为客户现场测试提供安全保障
- What is the difference between animators and animators- What is the difference between an Animator and an Animation?
- 3DMAX assign face map
- Spatial domain and frequency domain image compression of images
- 【10点公开课】:视频质量评价基础与实践
- 做接口测试都测什么?有哪些通用测试点?
- GPS from entry to abandonment (XIV), ionospheric delay
- RESNET rs: Google takes the lead in tuning RESNET, and its performance comprehensively surpasses efficientnet series | 2021 arXiv
- 0 basic learning C language - digital tube
- QT | UDP broadcast communication, simple use case
猜你喜欢
二分图判定
GPS从入门到放弃(十四)、电离层延时
A Mexican airliner bound for the United States was struck by lightning after taking off and then returned safely
Unity3d Learning Notes 6 - GPU instantiation (1)
RESNET rs: Google takes the lead in tuning RESNET, and its performance comprehensively surpasses efficientnet series | 2021 arXiv
数据处理技巧(7):MATLAB 读取数字字符串混杂的文本文件txt中的数据
Attack and defense world ditf Misc
重磅新闻 | Softing FG-200获得中国3C防爆认证 为客户现场测试提供安全保障
AI enterprise multi cloud storage architecture practice | Shenzhen potential technology sharing
Some problems about the use of char[] array assignment through scanf..
随机推荐
Some problems about the use of char[] array assignment through scanf..
GPS from entry to abandonment (XVII), tropospheric delay
Oracle-控制文件及日志文件的管理
Xiaoman network model & http1-http2 & browser cache
2021 geometry deep learning master Michael Bronstein long article analysis
Chapter 3: detailed explanation of class loading process (class life cycle)
[sdx62] wcn685x will bdwlan Bin and bdwlan Txt mutual conversion operation method
GPS from entry to abandonment (XIV), ionospheric delay
2022-07-05 使用tpcc对stonedb进行子查询测试
硬件開發筆記(十): 硬件開發基本流程,制作一個USB轉RS232的模塊(九):創建CH340G/MAX232封裝庫sop-16並關聯原理圖元器件
Seata聚合 AT、TCC、SAGA 、 XA事务模式打造一站式的分布式事务解决方案
414. The third largest digital buckle
GPS从入门到放弃(十八)、多路径效应
[MySQL] online DDL details
中国1,4-环己烷二甲醇(CHDM)行业调研与投资决策报告(2022版)
基於 QEMUv8 搭建 OP-TEE 開發環境
About the professional ethics of programmers, let's talk about it from the way of craftsmanship and neatness
CCNA Cisco network EIGRP protocol
Force buckle 575 Divide candy
2500个常用中文字符 + 130常用中英文字符