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 ...
随机推荐
- 【Offer】[68] 【树中两个结点的最低公共祖先】
题目描述 思路分析 测试用例 Java代码 代码链接 题目描述 输入两个树结点,求它们的最低公共祖先. [牛客网刷题地址]无 思路分析 该题首先要确定是否为二叉树,还要确定是否为二叉搜索树,是否有父指 ...
- [系列] go-gin-api 路由中间件 - 捕获异常(四)
概述 首先同步下项目概况: 上篇文章分享了,路由中间件 - 日志记录,这篇文章咱们分享:路由中间件 - 捕获异常.当系统发生异常时,提示 "系统异常,请联系管理员!",同时并发送 ...
- 《即时消息技术剖析与实战》学习笔记5——IM系统如何保证消息的一致性
一.什么是消息一致性 消息一致性指的是消息的时序一致性,即消息收发的一致性.如果不能保证时序一致性,就会造成聊天语义不连贯,引起误会. 对于点对点的聊天场景,时序一致性保证接收方的接收顺序和发送方的发 ...
- CNCF 宣布成立应用交付领域小组,正式开启云原生应用时代
作者|赵钰莹 作为云原生领域的顶级开源社区, Cloud Native Computing Foundation (云原生基金会,以下简称 CNCF)近日宣布成立 Application Delive ...
- 6、二叉树树(java实现)
1.创建树的节点 public class Node { public Object data; //存储数据 public Node leftChild; //左子树指针 public Node r ...
- 2、顺序表的实现(java代码)
1.这里实现了简单的顺序表的,为空判断.是否已满判断,插入.删除,查询元素下标等功能 public class Linear_List { private int[] arr; //用来保存数据 pr ...
- 原生JavaScript时间倒计时的方法
这个思路是来源用%的方法来做的: 以前用%做过转秒的 现在用来做倒计时方法: 需要用到的方法是getTime:获取距离1970年1月1日午夜00:00之间的毫秒差: var targetTime=ne ...
- PythonI/O进阶学习笔记_6.对象引用,可变性和垃圾回收
前言: 没有前言了- -......这系列是整理的以前的笔记上传的,有些我自己都忘记我当时记笔记的关联关系了. 记住以后 笔记记了就是用来复习的!!!不看不就啥用没了吗!!! content: 1.p ...
- .netCore部署在IIS上遇到的问题(500.19,500.21错误)
1.确保IIS功能都安装上了. 2.确保.netcore 的最新sdk已安装. 3.应用程序池改成无托管代码 4.500.19错误 错误原因,没有安装 DotNetCore.2.0.5-Windows ...
- 2019windows上安装Mac OS 10.14过程详细截图
之前VMware12里面的Mac OS10.10升级后,键盘鼠标就用不了了.试了几次都这样,只能重装VMware14, 安装Mac OS 10.14系统.把步骤截下图,分享一下. 一.材料准备 1.虚 ...