当前位置:网站首页>Leetcode剑指offer JZ9 双栈实现队列
Leetcode剑指offer JZ9 双栈实现队列
2022-07-24 05:22:00 【喜乐自在】

利用栈的特性(FILO)即先进后出原则,遇到熟悉的数据结构,有概念但没思路的话,就采用模拟的思想,如下图所示:
将stack1作为入队栈,stack2作为出队栈
#include<iostream>
using namespace std;
class Solution
{
public:
void push(int node) {
stack1.push(node)
}
int pop() {
if(stack2.empty()){
stack2.push(stack1.top()); //将栈1的顶部压入栈2中
stack1.pop(); //已经压入栈2的数据出栈1;
}
int a=stack2.top(); //用a保存栈2的顶部数据
stack2.pop(); //完成模拟队列顺序出栈
return a;
}
private:
stack<int> stack1;
stack<int> stack2;
};边栏推荐
- Write the list to txt and directly remove the comma in the middle
- day1-jvm+leetcode
- Traditional K-means implementation
- Hololens 2 development: development environment deployment
- 异地远程连接在家里的群晖NAS【无公网IP,免费内网穿透】
- Openpose2d transform 3D pose recognition
- Unity 3D frame rate statistics script
- 【无需公网IP】为远程桌面树莓派配置固定的公网TCP端口地址
- Hololens 2 development 101: create the first hololens 2 Application
- JDBC advanced -- learning from Shang Silicon Valley (DAO)
猜你喜欢
随机推荐
What is monotonic queue
Xshell remote access tool
记一次高校学生账户密码的获取,从无到有
Calculation steps of principal component analysis
IP课总结(3)
Dameng database_ Trigger, view, materialized view, sequence, synonym, auto increment, external link and other basic operations
IP笔记(10)
Statistical analysis of catering data --- Teddy cloud course homework
EXCEL 生成mysql语句批量操作
快速简单搭建FTP服务器,并内网穿透实现公网访问【无需公网IP】
How does the latest version of text (TMP) UI text of unity display in Chinese
Unity Shader :实现漫反射与高光反射
Unity(三)三维数学和坐标系统
如何建立一个仪式感点满的网站,并发布到公网 1-2
unity最新版本的Text(TMP)UI文本怎么显示中文
Hit the wall record (continuously updated)
[principles of database system] Chapter 4 advanced database model: Unified Modeling Language UML, object definition language ODL
Dameng database_ Various methods of connecting databases and executing SQL and scripts under disql
day5-jvm
头歌 平台作业









