题目

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. 使用docker save load 的时候的一个小问题

    当你使用docker save image_id > aa.tar ; 然后再使用 docker load < aa.tar 时, 你会发现此时导入的镜像的repository和 tag ...

  2. Kaggle八门神器(一):竞赛神器之XGBoost介绍

    Xgboost为一个十分有效的机器学习模型,在各种竞赛中均可以看到它的身影,同时Xgboost在工业届也有着广泛的应用,本文以Titanic数据集为研究对象,简单地探究Xgboost模型建模过程,同时 ...

  3. android应用开发全程实录-你有多熟悉listview?

    今天给大家带来<android应用开发全程实录>中关于listview和adatper中的部分.包括listview的基本使用,listview的优化等. 我们经常会在应用程序中使用列表的 ...

  4. Echarts的重点

    官网中,主要看文档的”教程“和”配置项手册“这两部分 1 下载 引入js 页面放一个容器,一定要设宽高 创建对象:var myChart = echarts.init(document.getElem ...

  5. null、undefined和NaN的区别

    未定义的值和定义未赋值的值是undefined: null是一种特殊的Object,可以给变量赋一个值null,来清除变量的值: NaN是一种特殊的number:

  6. 在使用seek()函数时,有时候会报错为 “io.UnsupportedOperation: can't do nonzero cur-relative seeks”,代码如下:

    __author__ = 'ZHHT' #!/usr/bin/env python # -*- coding:utf-8 -*- import os f = open("test1" ...

  7. JS移动端浏览器取消右划后退的几种方法

    在开发过程中,发现我们公司所使用的APP有点BUG,在APP中打开网页.H5应用之后,处于首页时,轻微的右划触发了后退事件,导致直接退出网页或者H5应用的页面,这样使得很多需要交互的手势没办法使用.本 ...

  8. C# 初始学习心情

    当听说需要转做.net的时候.内心是忐忑不安的.因为突然从前端转向后端,几乎完全颠倒了...一个注重界面实现功能.一个注重逻辑的开发,然并卵,服从需求吧. 虽说公司需要你转.但是时间是不允许的,所以只 ...

  9. python中__file__

    用__file__ 来获得脚本所在的路径,比如文件在/root下 cat tee #!/usr/bin/env pythonprint __file__              #得到相对路径tee ...

  10. Exchange 用户邮箱导入/导出

    在第2部分中,我将向您介绍如何使用Exchange Server中提供的新cmdlet导入/导出数据,以及如何查看导入和导出的信息统计信息这样做. 走起! 将数据从PST文件导入到邮箱 现在是时候尝试 ...