当前位置:网站首页>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;
};
边栏推荐
- Software testing interview reply: the technical side is not difficult for me, but the HR side is a hang up
- Database overview
- 【MATLAB】MATLAB图像处理基本操作
- [matlab] image compression coding (DCT, RLE)
- Video compression data set TVD
- [traffic sign recognition] Based on Matlab GUI YCbCr feature extraction +bp neural network traffic sign recognition [including Matlab source code 1869]
- [3.delphi common components] 6 scroll bar
- Treatment of small fish
- npm ERR Fix the upstream dependency conflict, or retry
- Clip paper details
猜你喜欢

Analysis of the difficulties in the architecture design of massive chat messages in the live broadcast room

双面材质【double sided】的Shader

Understand the role of before and after Clearfixafter clear floating

软件测试面试复盘:技术面没有难倒我,hr面却是一把挂

Go develop web

大厂测试员年薪30万到月薪8K,吐槽工资太低,反被网友群嘲?

Metersphere tutorial: how to assert when the returned result of the interface is null

Deep exploration of functions with indefinite parameters in C language

Data and electricity course design: circuit of full adder / subtractor

In the past 10 years, from zero foundation testing to test architect, he has made himself successful
随机推荐
Shader of double sided material
Orcale driver
Union find
In May, the main growth ranking list (platform of station B) of the single flying melon data up was released
[BSP video tutorial] BSP video tutorial issue 17: single chip microcomputer bootloader topic, startup, jump configuration and various usage of debugging and downloading (2022-06-10)
Number of different paths
SSH配置密钥登录时需要注意私钥是否设置了密码(passphrase)
技术分享| 快对讲,全球对讲
Coordinates of the capital of each province in China
记录一下我的刷题实录
Can the soft exam certificate be settled in Shanghai? Many people don't know
中國各省份省會的坐標
安全生产月知识竞赛——新安法知多少
cannot import name ‘dtensor‘ from ‘tensorflow. compat. v2.experimental‘
Virtual joystick of QT quick QML instance
Basic underlying principles of concurrent programming (4)
---Arrange numbers---
软测人都该知道的七大原则
Record the actual record of my question brushing
ACM tutorial - heap sorting