当前位置:网站首页>407 stack and queue (232. implementing queue with stack, 225. implementing stack with queue)
407 stack and queue (232. implementing queue with stack, 225. implementing stack with queue)
2022-06-23 07:07:00 【liufeng2023】
232. Using stack to realize queue

class MyQueue {
public:
stack<int> st_in;
stack<int> st_out;
MyQueue() {
}
void push(int x) {
st_in.push(x);
}
int pop() {
if (st_out.empty())
{
while (!st_in.empty())
{
st_out.push(st_in.top());
st_in.pop();
}
}
int result = st_out.top();
st_out.pop();
return result;
}
int peek() {
int res = this->pop();
st_out.push(res);
return res;
}
bool empty() {
return st_in.empty() && st_out.empty();
}
};

225. Using queue to implement stack
Method 1
class MyStack {
public:
queue<int> que1;
queue<int> que2;
public:
MyStack() {
}
void push(int x) {
que1.push(x);
}
int pop() {
int size = que1.size();
size--;
while (size--) take que1 Import que2, But leave the last element
{
que2.push(que1.front());
que1.pop();
}
int result = que1.front();// The last element left is the value to return
que1.pop();
que1 = que2; // then que2 Assign a value to que1
while (!que2.empty()) // Empty que2
{
que2.pop();
}
return result;
}
int top() {
return que1.back(); // The top element of the stack is que1 The last element inserted !
}
bool empty() {
return que1.empty();
}
};

Optimize : Method 2
In fact, this topic uses A line That's enough .
class MyStack {
public:
queue<int> que;
/** Initialize your data structure here. */
MyStack() {
}
/** Push element x onto stack. */
void push(int x) {
que.push(x);
}
/** Removes the element on top of the stack and returns that element. */
int pop() {
int size = que.size();
size--;
while (size--) {
// The element of the queue header ( Except for the last element ) Re add to the end of the queue
que.push(que.front());
que.pop();
}
int result = que.front(); // At this point, the order of the pop-up elements is the order of the stack
que.pop();
return result;
}
/** Get the top element. */
int top() {
return que.back();
}
/** Returns whether the stack is empty. */
bool empty() {
return que.empty();
}
};

边栏推荐
- [bull Chinese document] queue package used to process distributed jobs and messages in nodejs
- Concepts and differences of DQL, DML, DDL and DCL
- Storage mode of data in memory (C language)
- Idea installing the cloudtoolkit plug-in
- 300. 最长递增子序列
- mysql 优化
- 746. 使用最小花费爬楼梯-动态规划
- [STL] summary of deque usage of sequential containers
- ssm + ftp +ueditor
- 产品-Axure9(英文版),原型设计 制作下拉二级菜单
猜你喜欢

Open source oauth2 framework for SSO single sign on

直播回顾 | 传统应用进行容器化改造,如何既快又稳?

Intentional shared lock, intentional exclusive lock and deadlock of MySQL

Common setup modes (Abstract Factory & responsibility chain mode & observer mode)

GIS实战应用案例100篇(七十九)-多规整合底图的制作要点

聚焦行业,赋能客户 | 博云容器云产品族五大行业解决方案发布

MySQL redo log redo log

Storage mode of data in memory (C language)

ssm + ftp +ueditor
![[STL] summary of map usage of associated containers](/img/1d/1b6488ea47face0548500b1e1ec60d.png)
[STL] summary of map usage of associated containers
随机推荐
MySQL重做日志 redo log
[STL] unordered of associated container_ Map Usage Summary
MySQL optimization
关于#sql#的问题:有没有不增加字段,在原有字段的基础上,对字段里面的null值进行填充的方法呢
Mongodb record
Vs2013 ffmpeg environment configuration and common error handling
C # how to obtain DPI and real resolution (can solve the problem that has been 96)
core. What is JS ---kalrry
C language operator priority formula
TP6+Redis+think-queue+Supervisor实现进程常驻消息队列/job任务
core.js是什么---kalrry
How to migrate virtual machines from VirtualBox to hype-v
SSM整合
Endnote20 tutorial sharing (unfinished
306. 累加数
English grammar_ Adverb - ever / once
MySQL redo log redo log
MySQL index
Detailed explanation of callback function
MySQL MVCC多版本并发控制