当前位置:网站首页>【Leetcode】232. Implement queue with stack
【Leetcode】232. Implement queue with stack
2022-07-18 23:17:00 【LuZhouShiLi】
【Leetcode】232. Using stack to realize queue
subject
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
Ideas
- Stack in operation : Data can be directly put into the input stack
- The stack, : If the output stack is empty , Then directly send all the data in the input stack to the output stack , Then out of the stack , Achieve the goal of first in, first out , If the output stack is not empty , First pop up all the data in the output stack , Then proceed as above
- The stack is empty : Neither the input stack nor the output stack has data .
Code
class MyQueue {
public:
stack<int> stIn;
stack<int> stOut;
MyQueue() {
}
void push(int x) {
stIn.push(x);// Queue in operation Just enter the stack directly
}
int pop() {
// First judge whether the output stack is empty
if(stOut.empty()){
// If it's empty Import all input station data
while(!stIn.empty())
{
stOut.push(stIn.top());// Import the input stack elements
stIn.pop();// Input stack elements out of stack
}
}
int result = stOut.top();// Out of the stack
stOut.pop();
return result;
}
int peek() {
// Take the top element of the stack
int res = this->pop();
stOut.push(res);// because pop Function pops up the element res Add it back
return res;
}
bool empty() {
return stIn.empty() && stOut.empty();// Only the input stack and output stack are all empty It's empty
}
};
/** * 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(); */
边栏推荐
猜你喜欢

错误: -source 1.6 中不支持 diamond 运算符的解决办法

About products | how to plan products?

Beijing Hualian BHG mall continues to make efforts, and BHG day leads the new upsurge of urban consumption

Dialogue with machines, Ali Dharma academy challenges the new generation of man-machine dialogue Technology

20220715 domestic CONDA does not FQ the method of installing the latest version of pytoch

三维点云课程(一)——点云基础介绍

【开发教程1】开源蓝牙心率防水运动手环-套件检测教程

Leetcode -- 49 letter ectopic word grouping

C # - adding thread, loading case of progress bar, adding video effect for the first time

sklearn线性回归拟合一次项函数
随机推荐
20220715 domestic CONDA does not FQ the method of installing the latest version of pytoch
ASP. Net printing industry printing management system, source code free sharing
Picasso, an efficient search generalization sparse training solution
Original Rexroth proportional valve 4wrba10w64-2x/g24n9z4/m
Log collection scheme efk
The position of the lines drawn by canvas is disordered
vi编辑器设置自定义快捷键自动生成c语言的main函数
Learning record: FSMC - extended external SRAM
【C语言】strlen函数的讲解和模拟实现
App的回归测试,有什么高效的测试方法?
Is there any prospect for individuals to do overseas social media? Any good suggestions?
Leetcode -- intersection of two arrays 2
Collaboration process [simple summary]
20220714给AIO-3568J适配OpenHarmony-v3.1-beta(编译Buildroot)
Solution to Chinese garbled code in response results of burpsuite tool
No cl.exe solution found
MySQL related commands
【Flutter--实战】Dart 语言快速入门
【百度飞桨】手写数字识别模型部署Paddle Inference
Progress [detailed summary]