题目

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. Linux用脚本守护进程

    while true; do server=`ps -aux | grep tomcat | grep -v grep` if [ ! "$server" ]; then echo ...

  2. 各种安卓模拟器连接Adb

    夜神模拟器:adb connect 127.0.0.1:62001 逍遥安卓模拟器:adb connect 127.0.0.1:21503 天天模拟器:adb connect 127.0.0.1:65 ...

  3. 常用的http网页错误代码表---------495引发的一个简单到爆,但基于国内环境只能呵呵呵的血案

    敲代码敲出了个网页错误代码 495. 然后,正常的跑去百度,看了一堆还是没有完整的网页错误代码,应该说国内的环境的网页错误代码表只有官方的那几个,那么只能FQ了. 去到谷歌,一查全是俄语,乐了,明白是 ...

  4. webstorm增加内存配置参数

    webstorm增加内存配置参数 找到WebStorm.exe.vmoptions这个文件,路径如下 webstorm安装主目录>bin>WebStorm.exe.vmoptions 更改 ...

  5. The first step in solving any problem is recognizing there is one.

    The first step in solving any problem is recognizing there is one.解决问题的第一步是要承认确实存在问题.

  6. 如何提高Mysql的查询效率???

    1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引. 2.应尽量避免在 where 子句中对字段进行 null 值判断,否则将导致引擎放弃使用索 ...

  7. dstat工具使用介绍

    一.dstat工具多功能系统资源统计生成工具.获取信息类似top.free.iostat.vmstat等多个工具的合集,所以也称为vmstat.iostat.ifstat等工具替代品,其结果可以存储成 ...

  8. My sql之存储过程+游标

    sql 实例如下: /**************定义更改car_station_user_acct_his new_balance old_balance存储过程**************/ cr ...

  9. 基于FPGA的DDS任意波形发生器设计

    一.简介       DDS技术最初是作为频率合成技术提出的,由于其易于控制,相位连续,输出频率稳定度高,分辨率高, 频率转换速度快等优点,现在被广泛应用于任意波形发生器(AWG).基于DDS技术的任 ...

  10. 洛谷 P3143 [USACO16OPEN]钻石收藏家Diamond Collector

    题目描述 Bessie the cow, always a fan of shiny objects, has taken up a hobby of mining diamonds in her s ...