LeetCode_225-Implement Stack using Queues
使用队列实现栈的操作
class MyStack {
public:
/** Initialize your data structure here. */
MyStack() {
}
/** Push element x onto stack. */
void push(int x) {
if(!queueA.empty()) {
queueA.push(x);
}
else {
queueB.push(x);
}
}
/** Removes the element on top of the stack and returns that element. */
int pop() {
int nTmp = ;
if(queueA.empty()) {
while(queueB.size() > ) {
queueA.push(queueB.front());
queueB.pop();
}
nTmp = queueB.front();
queueB.pop();
}
else {
while(queueA.size() > ) {
queueB.push(queueA.front());
queueA.pop();
}
nTmp = queueA.front();
queueA.pop();
}
return nTmp;
}
/** Get the top element. */
int top() {
int nTmp = ;
if(queueA.empty()) {
while(!queueB.empty()) {
nTmp = queueB.front();
queueA.push(queueB.front());
queueB.pop();
}
}
else {
while(!queueA.empty()) {
nTmp = queueA.front();
queueB.push(queueA.front());
queueA.pop();
}
}
return nTmp;
}
/** Returns whether the stack is empty. */
bool empty() {
if(queueA.empty() && queueB.empty()) {
return true;
}
return false;
}
protected:
queue<int> queueA;
queue<int> queueB;
};

可关注公众号了解更多的面试技巧
LeetCode_225-Implement Stack using Queues的更多相关文章
- 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 ...
- Implement Queue by Two Stacks & Implement Stack using Queues
Implement Queue by Two Stacks Implement the following operations of a queue using stacks. push(x) -- ...
- [LeetCode] Implement Stack using Queues 用队列来实现栈
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
- Java for LeetCode 225 Implement Stack using Queues
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
- Implement Stack using Queues
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
- (leetcode)Implement Stack using Queues
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
- (easy)LeetCode 225.Implement Stack using Queues
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
随机推荐
- 【第十七篇】easyui-datagrid 导出Excel (在客户端能弹出下载框)
//导出Excel function exportExcel(obj) { var SaleOrderNo = $("#SaleOrderNo").val().trim(); va ...
- cobbler高可用方案
一.环境准备 主网IP 私网IP 主机名 角色 VIP 10.203.178.125 192.168.10.2 cnsz22VLK12919 主 10.203.178.137,192.168.10.1 ...
- Winform中使用FastReport实现自定义PDF打印预览
场景 Winform中使用FastReport实现简单的自定义PDF导出: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/1009 ...
- apache ignite系列(四):持久化
ignite持久化与固化内存 1.持久化的机制 ignite持久化的关键点如下: ignite持久化可防止内存溢出导致数据丢失的情况: 持久化可以定制化配置,按需持久化; 持久化能解决在大量缓存数据情 ...
- Redis压缩包win10快速启动之记录一
转载请标明出处: http://dujinyang.blog.csdn.net/ 本文出自:[奥特曼超人的博客] Redis压缩包 配置环境变量,直接CMD中启动,默认是打开redis.conf,当然 ...
- Linux 笔记 - 第十章 Shell 基础知识
博客地址:http://www.moonxy.com 一.前言 Shell 是系统的用户界面,提供了用户与内核进行交互操作的一种接口,它接收用户输入的命令并把它送入内核去执行.实际上 Shell 是一 ...
- Flink 从 0 到 1 学习 —— 如何自定义 Data Sink ?
前言 前篇文章 <从0到1学习Flink>-- Data Sink 介绍 介绍了 Flink Data Sink,也介绍了 Flink 自带的 Sink,那么如何自定义自己的 Sink 呢 ...
- jmeter 查看结果树数据分析 优化
1.点击查看结果树,配置 2.筛选功能项
- PyCharm2019激活
PyCharm下载地址:https://www.jetbrains.com/pycharm/download/ 永久激活 这里主要介绍永久激活的方式,永久激活后,就可以放心使用了,一劳永逸,5分钟就能 ...
- vue-cli+webpack打包,上线
1.先修改配置文件再打包.有些人打包后运行一片空白,主要是由于路径问题 所以首先需要修改config下的index.js配置文件 上图中第一个要修改的就是静态文件的路径,打包后静态文件就在当前目录下, ...