当前位置:网站首页>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(); */
边栏推荐
- 每日一题:力扣:225:用队列实现栈
- 十二、启动流程
- Shell product written examination related
- Embedded common computing artifact excel, welcome to recommend skills to keep the document constantly updated and provide convenience for others
- GPS from getting started to giving up (XVIII), multipath effect
- Data processing skills (7): MATLAB reads the data in the text file TXT with mixed digital strings
- Data storage (1)
- MongoDB(三)——CRUD
- Shortcut keys in the terminal
- What is the difference between animators and animators- What is the difference between an Animator and an Animation?
猜你喜欢
![[leetcode daily clock in] 1020 Number of enclaves](/img/2d/3d12f20c8c73fb28044c01be633c99.jpg)
[leetcode daily clock in] 1020 Number of enclaves

CCNA Cisco network EIGRP protocol

2020 Bioinformatics | GraphDTA: predicting drug target binding affinity with graph neural networks

PVL EDI project case

GPS from entry to abandonment (XVII), tropospheric delay

Data processing skills (7): MATLAB reads the data in the text file TXT with mixed digital strings

Management background --2 Classification list

小常识:保险中的“保全”是什么?

GPS from getting started to giving up (XIII), receiver autonomous integrity monitoring (RAIM)

GPS从入门到放弃(十七) 、对流层延时
随机推荐
CCNA Cisco network EIGRP protocol
小常识:保险中的“保全”是什么?
Problems in the process of opencv300 cmake generating project
Xiaoman network model & http1-http2 & browser cache
[MySQL] online DDL details
2022-07-05 使用tpcc对stonedb进行子查询测试
2020 Bioinformatics | GraphDTA: predicting drug target binding affinity with graph neural networks
Unity3D学习笔记6——GPU实例化(1)
MariaDB database management system learning (I) installation diagram
GNN, please deepen your network layer~
Save and retrieve strings
Seata aggregates at, TCC, Saga and XA transaction modes to create a one-stop distributed transaction solution
2022年6月国产数据库大事记-墨天轮
Research and investment strategy report of China's VOCs catalyst industry (2022 Edition)
Oracle Performance Analysis 3: introduction to tkprof
HDU 2008 数字统计
Search element topic (DFS)
labelimg的安装与使用
Codeforces Round #274 (Div. 2) –A Expression
中国VOCs催化剂行业研究与投资战略报告(2022版)