当前位置:网站首页>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(); */
边栏推荐
- QT | UDP broadcast communication, simple use case
- Insert sort and Hill sort
- GPS from getting started to giving up (16), satellite clock error and satellite ephemeris error
- 基于 QEMUv8 搭建 OP-TEE 开发环境
- Learn the principle of database kernel from Oracle log parsing
- 2021 geometry deep learning master Michael Bronstein long article analysis
- 十二、启动流程
- C # réalise la liaison des données du rapport Crystal et l'impression du Code à barres 4
- MariaDB database management system learning (I) installation diagram
- Embedded common computing artifact excel, welcome to recommend skills to keep the document constantly updated and provide convenience for others
猜你喜欢
The nearest common ancestor of binary (search) tree ●●
RESNET rs: Google takes the lead in tuning RESNET, and its performance comprehensively surpasses efficientnet series | 2021 arXiv
GPS从入门到放弃(十六)、卫星时钟误差和卫星星历误差
Some problems about the use of char[] array assignment through scanf..
UNI-Admin基础框架怎么关闭创建超级管理员入口?
A Mexican airliner bound for the United States was struck by lightning after taking off and then returned safely
[MySQL] online DDL details
Learn the principle of database kernel from Oracle log parsing
GPS from getting started to giving up (XIII), receiver autonomous integrity monitoring (RAIM)
[linear algebra] determinant of order 1.3 n
随机推荐
About the professional ethics of programmers, let's talk about it from the way of craftsmanship and neatness
二叉(搜索)树的最近公共祖先 ●●
Crawler obtains real estate data
Bat script learning (I)
Some problems about the use of char[] array assignment through scanf..
Kohana database
Common sense: what is "preservation" in insurance?
ResNet-RS:谷歌领衔调优ResNet,性能全面超越EfficientNet系列 | 2021 arxiv
QT | UDP broadcast communication, simple use case
GPS从入门到放弃(十一)、差分GPS
Management background --5, sub classification
GPS从入门到放弃(十九)、精密星历(sp3格式)
Lora sync word settings
Maximum product of three numbers in question 628 of Li Kou
Set status bar style demo
OpenCV300 CMake生成project在项目过程中的问题
NetXpert XG2帮您解决“布线安装与维护”难题
0 basic learning C language - digital tube
Oracle性能分析3:TKPROF简介
Learn the principle of database kernel from Oracle log parsing