当前位置:网站首页>Implementing queues with stacks
Implementing queues with stacks
2022-06-11 02:13:00 【Lingling Ling】
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/implement-queue-using-stacks
Title Description :
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
Input :
[“MyQueue”, “push”, “push”, “peek”, “pop”, “empty”]
[[], [1], [2], [], [], []]
Output :
[null, null, null, 1, 1, false]
Their thinking :
No matter what structure the bottom layer is implemented with , A logical data structure that always guarantees data first in first out , That's the queue
Create a stack st1 and st2, Join the team directly in the stack st1 Join the team ,
When the team , if st2 Not empty , Directly out of the stack is out of the team , if st2 Already empty , Then the stack st1 Import all the data inside st2
When getting the team head element , if st2 Not empty , Go to the top of the stack and the element is the team leader , If it is empty , The stack st1 Import all the data inside st2
Determines if the queue is empty , The entire queue is empty only when both stacks are empty
class MyQueue {
public:
MyQueue() {
}
void push(int x) {
st1.push(x);
}
int pop() {
/* stack<int>* emptyS = &st1; stack<int>* nonemptyS = &st2; if(!st1.empty()) { swap(emptyS,nonemptyS); } int top = nonemptyS->top(); nonemptyS->pop(); return top; */
int retval;
if(!st2.empty())
{
retval = st2.top();
st2.pop();
return retval;
}
while(!st1.empty())
{
st2.push(st1.top());
st1.pop();
}
retval = st2.top();
st2.pop();
return retval;
}
int peek() {
/* if(!st1.empty()) { // int top1 = st1.top(); //st1.pop(); st2.push(st1.top()); return st2.top(); } else { // int top2 = st2.top(); // st2.pop(); st1.push(st2.top()); return st1.top(); } */
int retval;
if(!st2.empty())
{
retval = st2.top();
return retval;
}
while(!st1.empty())
{
st2.push(st1.top());
st1.pop();
}
return st2.top();
}
bool empty() {
return st1.empty() && st2.empty();
}
private:
stack<int> st1;
stack<int> st2;
};
边栏推荐
- [matlab] image segmentation
- Shell learning tutorial (super detailed and complete)
- Go develop web
- Wrong question (character array)
- JS Part 5
- ASEMI场效应管12N65参数,12N65规格书,12N65特征
- ME11/ME12采购信息记录及条件记录创建及更新BAPI:ME_INFORECORD_MAINTAIN_MULTI
- 27岁女生零基础转行软件测试,合适吗?
- npm ERR Fix the upstream dependency conflict, or retry
- Polynomial multiplication
猜你喜欢

SAP SMARTFORMS文本内容手动换行输出

CRS-4544 & ORA-09925

Union find

腾讯面试官曰Mysql架构的内部模块索引原理及性能优化思路谁会?

Secret

During SSH configuration key login, you need to pay attention to whether the private key is set with a password

---Arrange numbers---

SSH配置密钥登录时需要注意私钥是否设置了密码(passphrase)

AI fanaticism | come to this conference and work together on the new tools of AI!
![Is it correct to declare an array in this way? int n=10,a[n]; What if so? const int n =10; int a[n];](/img/e9/b09151d9b92d0a216e2ba079a7beb4.jpg)
Is it correct to declare an array in this way? int n=10,a[n]; What if so? const int n =10; int a[n];
随机推荐
【Qt】error: QApplication: No such file or directory 解决方案
安全生产月知识竞赛——新安法知多少
SAP smartforms text content manual wrap output
SAP SMARTFORMS换页打印自动判断
从测试零基础到测试架构师,这10年,他是这样让自己成才的
Virtual joystick of QT quick QML instance
20N10-ASEMI中小功率MOS管20N10
Wrong question (character array)
Return function of different return values
QT database learning notes (I) basic concepts of database
Understanding of pointers
[C language] storage of data in memory -1 plastic
Programming implementation: input any English month, and output its corresponding Chinese prompt after looking up the month table. Abbreviations can also be found.
浅析直播间海量聊天消息的架构设计难点
Start with interpreting the code automatically generated by BDC, and explain the trial version of the program components of sapgui
[3.delphi common components] 6 scroll bar
cannot import name ‘dtensor‘ from ‘tensorflow. compat. v2.experimental‘
How to change the administrator's Avatar in win11? Win11 method of changing administrator image
Sword finger offer II 095 Longest common subsequence dynamic programming
ACM tutorial - heap sorting