题目

Implement the following operations of a stack using queues.

push(x) – Push element x onto stack.

pop() – Removes the element on top of the stack.

top() – Get the top element.

empty() – Return whether the stack is empty.

Notes:

You must use only standard operations of a queue – which means only push to back, peek/pop from front, size, and is empty operations are valid.

Depending on your language, queue may not be supported natively. You may simulate a queue by using a list or deque (double-ended queue), as long as you use only standard operations of a queue.

You may assume that all operations are valid (for example, no pop or top operations will be called on an empty stack).

分析

用队列实现栈。

用两个队列,其中一个队列用户存储当前元素,另一个辅助队列作为pop和top操作时的临时存储。并利用flag标志,表示存储当前所有元素的队列。

AC代码

class Stack {
public:
// Push element x onto stack.
void push(int x) {
que[flag].push(x);
} // Removes the element on top of the stack.
void pop() {
while (que[flag].size() > 1)
{
que[1 - flag].push(que[flag].front());
que[flag].pop();
}//while
que[flag].pop();
flag = 1 - flag;
} // Get the top element.
int top() {
while (que[flag].size() > 1)
{
que[1 - flag].push(que[flag].front());
que[flag].pop();
}//while
int ret = que[flag].front();
que[1 - flag].push(que[flag].front());
que[flag].pop(); flag = 1 - flag; return ret;
} // Return whether the stack is empty.
bool empty() {
return que[flag].empty();
} private:
queue<int> que[2];
int flag = 0; //作为存储队列
};

GitHub测试程序源码

LeetCode(225) Implement Stack using Queues的更多相关文章

  1. LeetCode(28)Implement strStr()

    题目 Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if nee ...

  2. LeetCode(232) Implement Queue using Stacks

    题目 Implement the following operations of a queue using stacks. push(x) – Push element x to the back ...

  3. LeetCode(155) Min Stack

    题目 Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. ...

  4. 【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( ...

  5. 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 ...

  6. leetcode:Implement Stack using Queues 与 Implement Queue using Stacks

    一.Implement Stack using Queues Implement the following operations of a stack using queues. push(x) - ...

  7. 232. Implement Queue using Stacks,225. Implement Stack using Queues

    232. Implement Queue using Stacks Total Accepted: 27024 Total Submissions: 79793 Difficulty: Easy Im ...

  8. 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) -- ...

  9. LeetCode(275)H-Index II

    题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...

随机推荐

  1. jsp中c标签的使用

    jsp中c标签的使用 核心标签库 它是JSTL中的核心库,为日常任务提供通用支持,如显示和设置变量.重复使用一组项目.测试条件和其他操作(如导入和重定向Web内容).Core标签按功能可分为4种类型: ...

  2. 老男孩IT教育-每日一题汇总

    老男孩IT教育-每日一题汇总 第几天 第几周 日期 快速访问链接 第123天 第二十五周 2017年8月25日 出现Swap file….already exists以下错误如何解决? 第122天 2 ...

  3. 运行node提示:events.js:160 throw er; // Unhandled 'error' event

    运行node时遇到下述提示: events.js:160  throw er; // Unhandled 'error' event或者events.js:160       throw er; // ...

  4. Column 'xxx' in field list is ambiguous

    一 其实看一下ambiguous的翻译就好了 一开始我觉得是含糊什么的,后来找了下才知道应该是双关... 二 所以翻译过来就是 : 列'XX'在字段列表中双关 其实就是两张表有相同的字段,但是使用时, ...

  5. fleet-运行一个全局的单元

    运行一个全局的单元 正如前面所提到的,全局单元是有用的,用于在您的集群中的所有机器上运行一个单元.它不会比一个普通的单元差太多,而是一个新的x-fleet参数称为Global=true.这是一个示例单 ...

  6. css3响应式图片

    响应式图片指用户代理根据输出设备的分辨率不同加载不同类型的图片,不会造成带宽的浪费. 同时,在改变输出设备类型或分辨率时,能及时加载对应类型的图片.   常用的实现方式: 1.用srcset和size ...

  7. Spring的DI(Dependency Injection)

    写在之前,作为正在学习的程序员,对于Spring理解比较差,给两个简单的定义大家看一下. 控制反转(Inversion of Control),是一个重要的面向对象编程的法则来削减计算机程序的耦合问题 ...

  8. react中constructor和super()以及super(props)的区别。

    react中这两个API出镜率超级高,但是一直不太懂这到底是干嘛的,有什么用:今天整理一下,方便自己查看同时方便大家. 1.constructor( )-----super( )的基本含义 const ...

  9. Win10微软帐户切换不回Administrator本地帐户的解决方法【亲测】

    在Win10系统中经常会用到微软帐户登录,如应用商店等地方,不过一些用户反馈原来使用Administrator帐户被绑定微软帐户后无法切换回本地帐户,连[改用本地帐户登录]按钮都没有,那么怎么解决呢? ...

  10. python基础教程总结13——网络编程,

    1.网络设计模块 1.1 socket模块    根据连接启动的方式以及本地套接字要连接的目标,套接字之间的连接过程可以分为三个步骤:服务器监听,客户端请求,连接确认. 1)服务器监听:是服务器端套接 ...