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 ...
随机推荐
- JS-DOM ~ 02. 隐藏二维码、锁定、获取输入框焦点、for循环为文本赋值、筛选条件、全选和反选、属性的方法操作、节点的层次结构、nodeType
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 宝塔Linux面板命令
安装宝塔 Centos安装脚本 yum install -y wget && wget -O install.sh http://download.bt.cn/install/inst ...
- 微信小程序集成腾讯云 IM SDK
微信小程序集成腾讯云 IM SDK 1.背景 因业务功能需求需要接入IM(即时聊天)功能,一开始想到的是使用 WebSocket 来实现这个功能,然天意捉弄(哈哈)服务器版本太低不支持 wx 协议(也 ...
- Java多线程(十三):线程池
线程池类结构 1.Executor是顶级接口,有一个execute方法. 2.ExecutorService接口提供了管理线程的方法. 3.AbstractExecutorService管理普通线程, ...
- asp.net core 使用 signalR(二)
asp.net core 使用 signalR(二) Intro 上次介绍了 asp.net core 中使用 signalR 服务端的开发,这次总结一下web前端如何接入和使用 signalR,本文 ...
- Webpack 打包太慢? 试试 Dllplugin
webpack在build包的时候,有时候会遇到打包时间很长的问题,这里提供了一个解决方案,让打包如丝般顺滑~ 1. 介绍 在用 Webpack 打包的时候,对于一些不经常更新的第三方库,比如 rea ...
- netty源码解解析(4.0)-22 ByteBuf的I/O
ByteBuf的I/O主要解决的问题有两个: 管理readerIndex和writerIndex.这个在在AbstractByteBuf中解决. 从内存中读写数据.ByteBuf的不同实现主要 ...
- windows下安装spark
1.安装jdk 2.安装scala 3.下载spark spark下载地址 3.1安装spark 将下载的文件解压到一个目录,注意目录不能有空格,比如说不能解压到C:\Program Files 作者 ...
- pathlib模块
一.pathlib库官方定义 pathlib 是Python内置库,Python 文档给它的定义是 Object-oriented filesystem paths(面向对象的文件系统路径).path ...
- 开源流媒体Red5-编译和部署
源码下载地址:https://github.com/Red5/red5-server 使用工具:IntelliJ IDEA 下载源码后直接用IDEA打开,等待全部加载完成后 编译看是否报错,应该没什么 ...