LeetCode_232-Implement Queue using Stacks
题意是使用栈实现队列;队列是先进先出,后进后出。
class MyQueue {
public:
/** Initialize your data structure here. */
MyQueue() {
//intput
m_bSwap = true;
}
/** Push element x to the back of queue. */
void push(int x) {
if(!m_bSwap) {
while(!m_soutput.empty()) {
m_sintput.push(m_soutput.top());
m_soutput.pop();
}
m_bSwap = true;
}
m_sintput.push(x);
}
/** Removes the element from in front of queue and returns that element. */
int pop() {
if(m_bSwap) {
while(!m_sintput.empty()) {
m_soutput.push(m_sintput.top());
m_sintput.pop();
}
m_bSwap = false;
}
int nNum = m_soutput.top();
m_soutput.pop();
return nNum;
}
/** Get the front element. */
int peek() {
if(m_bSwap) {
while(!m_sintput.empty()) {
m_soutput.push(m_sintput.top());
m_sintput.pop();
}
m_bSwap = false;
}
return m_soutput.top();
}
/** Returns whether the queue is empty. */
bool empty() {
if(m_sintput.empty() && m_soutput.empty()) {
return true;
}
return false;
}
protected:
stack<int> m_sintput;
stack<int> m_soutput;
bool m_bSwap;
};

可关注公众号了解更多的面试技巧
LeetCode_232-Implement Queue using Stacks的更多相关文章
- leetcode:Implement Stack using Queues 与 Implement Queue using Stacks
一.Implement Stack using Queues Implement the following operations of a stack using queues. push(x) - ...
- 【LeetCode】232 & 225 - Implement Queue using Stacks & Implement Stack using Queues
232 - Implement Queue using Stacks Implement the following operations of a queue using stacks. push( ...
- 232. Implement Queue using Stacks,225. Implement Stack using Queues
232. Implement Queue using Stacks Total Accepted: 27024 Total Submissions: 79793 Difficulty: Easy Im ...
- leetcode 155. Min Stack 、232. Implement Queue using Stacks 、225. Implement Stack using Queues
155. Min Stack class MinStack { public: /** initialize your data structure here. */ MinStack() { } v ...
- Lintcode: Implement Queue by Stacks 解题报告
Implement Queue by Stacks 原题链接 : http://lintcode.com/zh-cn/problem/implement-queue-by-stacks/# As th ...
- Leetcode 232 Implement Queue using Stacks 和 231 Power of Two
1. 232 Implement Queue using Stacks 1.1 问题描写叙述 使用栈模拟实现队列.模拟实现例如以下操作: push(x). 将元素x放入队尾. pop(). 移除队首元 ...
- LeetCode 232. 用栈实现队列(Implement Queue using Stacks) 4
232. 用栈实现队列 232. Implement Queue using Stacks 题目描述 使用栈实现队列的下列操作: push(x) -- 将一个元素放入队列的尾部. pop() -- 从 ...
- [LeetCode] Implement Queue using Stacks 用栈来实现队列
Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...
- Leetcode Implement Queue using Stacks
Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...
- Implement Queue using Stacks
Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...
随机推荐
- 实现一个简易版的vuex持久化工具
背景 最近用uni-app开发小程序项目时,部分需要持久化的内容直接使用vue中的持久化插件貌似不太行,所以想着自己实现一下类似vuex-persistedstate插件的功能,想着功能不多,代码量应 ...
- Django框架下的增强分页组件
本文通过文章同步功能推送至博客园,显示排版可能会有所错误,请见谅! 描述:Django框架内置了分页功能,但其只能满足简单需求,难以实现复杂功能. 实现代码: #!/usr/bin/env pytho ...
- Intro to Machine Learning
本节主要用于机器学习入门,介绍两个简单的分类模型: 决策树和随机森林 不涉及内部原理,仅仅介绍基础的调用方法 1. How Models Work 以简单的决策树为例 This step of cap ...
- Salesforce学习之路-admin篇
Salesforce是一款非常强大的CRM(Customer Relationship Management)系统,国外企业使用十分频繁,而国内目前仅有几家在使用(当然,国内外企使用的依旧较多),因此 ...
- sql server 2014 的安装
1.双击打开sql_server2014的安装包 2.点击弹出来的对话框的确定按钮 3.等待一会,安装包在准备中 4.弹出SQL server 安装中心,点击全新 SQL Server 独立安装 5. ...
- AMBA——slave的HREADY信号
在前几天的ARM面试中,被问到总线架构,主要是AMBA那一套东西.对于AMBA之前上课学过一点,但很肤浅.为了面试上网查了一下.也看了部分协议补充了一下,但是接触的少,理解的不深入,被问到之前没遇到的 ...
- 52 (OC)* 苹果手机各种尺寸详细表以及iPhoneX、iPhoneXS、iPhoneXR、iPhoneXSMax屏幕适配
iPhone设备 物理分辨率是硬件所支持的,逻辑分辨率是软件可以达到的. 代数 设备 操作系统 逻辑分辨率(point) 物理分辨率(pixel) 屏幕尺寸(对角线长度) 缩放因子 iPhone ...
- .NET Core 3.0即将发布!
期待已久的.NET Core 3.0即将发布! .NET Core 3.0在.NET Conf上发布.大约还有9个多小时后,.NET Conf开始启动. 为期3天的大概日程安排如下: 第1天-9月23 ...
- tcp居然会数据延迟40ms被发送
tcpdump是很好的tcp分析工具,在此配合nc命令来学习tcpdump nc -l 8000 tcpdump -S -n -i lo tcp and host 127.0.0.1 and port ...
- python中pyqt5的进度条--python实战(十)
python太博大精深了,使用场景非常多.最近笔者一直使用PyQt5编一些小程序,顺便就把一些常用的东西列出来,做个记录和积累吧.进度条是非常常用的东西,今天用的时候,顺便温习了一下,这个东西自己感觉 ...