当前位置:网站首页>The classic dual stack implementation queue, pay attention to the modification of the judgment conditions of traversing the stack.
The classic dual stack implementation queue, pay attention to the modification of the judgment conditions of traversing the stack.
2022-07-28 23:42:00 【dor.yang】
Topic content
Please use only two stacks to implement the FIFO queue . The queue should support all operations supported by the general queue (push、pop、peek、empty):
Realization MyQueue class :
void push(int x) Put the element x Push to the end of the queue
int pop() Remove from the beginning of the queue and return the element
int peek() Returns the element at the beginning of the queue
boolean empty() If the queue is empty , return true ; otherwise , return false
explain :
you Can only Use standard stack operations —— It's just push to top, peek/pop from top, size, and is empty Operation is legal .
The language you use may not support stacks . You can use list perhaps deque( deque ) To simulate a stack , As long as it's a standard stack operation .
Example 1:
Input :
[“MyQueue”, “push”, “push”, “peek”, “pop”, “empty”]
[[], [1], [2], [], [], []]
Output :
[null, null, null, 1, 1, false]
explain :
MyQueue myQueue = new MyQueue();
myQueue.push(1); // queue is: [1]
myQueue.push(2); // queue is: [1, 2] (leftmost is front of the queue)
myQueue.peek(); // return 1
myQueue.pop(); // return 1, queue is [2]
myQueue.empty(); // return false
Tips :
1 <= x <= 9
Call at most 100 Time push、pop、peek and empty
Suppose all operations are valid ( for example , An empty queue will not call pop perhaps peek operation )
source : Power button (LeetCode)
link :https://leetcode.cn/problems/implement-queue-using-stacks
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Their thinking
The stack is forward and out , Two stacks can naturally realize FIFO , We just need to input on a stack , Output on another stack , Only when the output stack is empty , We send all the elements of the input stack to the output stack , You can realize the first in, first out function of the queue . However, it should be noted here that all input stacks must be put into the output stack , Otherwise, it will be nothing .
The concrete implementation is really very simple , Just pay attention to one small point , That is, the traversal of the stack cannot be used in the determination of recycling a.size() In this format , Because this variable is variable in the loop , Easy to walk .
Code
class MyQueue {
public:
stack<int> a;
stack<int> b;
MyQueue() {
}
void push(int x) {
a.push(x);
}
int pop() {
if(b.size()==0){
int l=a.size();
for(int i=0;i<l;i++){
int num=a.top();
a.pop();
b.push(num);
}
}
int ans=b.top();
b.pop();
if(b.size()==0){
int l=a.size();
for(int i=0;i<l;i++){
int num=a.top();
a.pop();
b.push(num);
}
}
return ans;
}
int peek() {
if(b.size()==0){
int l=a.size();
for(int i=0;i<l;i++){
int num=a.top();cout<<num<<endl;
a.pop();
b.push(num);
}
}
return b.top();
}
bool empty() {
if(b.size()==0&&a.size()==0){
return true;
}
else{
return false;
}
}
};
/** * Your MyQueue object will be instantiated and called as such: * MyQueue* obj = new MyQueue(); * obj->push(x); * int param_2 = obj->pop(); * int param_3 = obj->peek(); * bool param_4 = obj->empty(); */
边栏推荐
- 如何将一个mongodb中集合的索引 添加到另一个mongodb中集合中
- Pin mapping relationship of stm32f103c series single chip microcomputer under Arduino framework
- 【自】-刷题-字符串
- Arduino框架下STM32F103C系列单片机引脚映射关系
- MySQL transaction and storage system
- 行泊一体迎爆发期,抢量产还是修技术护城河?
- Form label
- Wechat applet development ④
- 2022 welder (Junior) work license questions and answers
- 互动滑轨屏在展厅中应用的制作步骤
猜你喜欢

2022 simulated examination platform operation of hoisting machinery command examination questions

Meet the outbreak period with the integration of transportation and parking, rush for mass production or build a technical moat?
![[self] - question brushing - peak value](/img/cf/9c47da9c574b61415578e7fde8b126.png)
[self] - question brushing - peak value
![[self] - question brushing - string](/img/80/7d571e4f82caaad00d0a20152832c2.png)
[self] - question brushing - string

Few people can really play in the "aftermarket" of the whole house intelligent fire collection

零视科技 H5S视频平台 GetUserInfo 信息泄漏漏洞 CNVD-2020-67113

深度剖析集成学习Xgboost

Messages from students participating in the competition: memories of the 17th session

【自】-刷题-BFS

Shenkaihong: on the river of Zhilian of all things, there is a bright moon of open source
随机推荐
Mongodb index add, view, export, delete
What is utxo?
Text is hidden beyond and ellipsis is displayed
Go 中的并发 Concurrency
Trivy [3] custom scanning strategy
Mycms we media mall V3.6 release, compatible with micro engine application development (laravel framework)
【自】-刷题-动态规划
Few people can really play in the "aftermarket" of the whole house intelligent fire collection
通过Wi-Fi 7实现极高吞吐量——洞察下一代Wi-Fi物理层
金仓数据库KingbaseES 客户端编程接口指南 - ODBC特性支持约束
零视科技 H5S视频平台 GetUserInfo 信息泄漏漏洞 CNVD-2020-67113
Arduino框架下STM32F103C系列单片机引脚映射关系
MySQL introduction
Custom MVC principle and framework
Function function
My second uncle is angry and swipes the screen all over the network. How can he cure my spiritual internal friction?
trivy【3】自定义扫描策略
Solve the problem of using anonymous users in pod due to the failure of attaching ciphertext token files for serviceaccount user authentication
如何将一个mongodb中集合的索引 添加到另一个mongodb中集合中
深度剖析集成学习Adaboost