题目

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. JavaSE---基本网络支持

    1.Java为网络支持提供了java.net包,该包下的URL和URLConnection等类提供了 以编程方式访问WEB服务 的功能: 2.URLDecoder.URLEncoder提供了 普通字符 ...

  2. (转)linux traceroute命令参数及用法详解--linux跟踪路由命令

    linux traceroute命令参数及用法详解--linux跟踪路由命令 原文:http://blog.csdn.net/liyuan_669/article/details/25362505 通 ...

  3. spring的2种类型转换器

    spring有2种类型转换器,一种是propertyEditor,一种是Converter.虽然都是类型转换,但是还是有细微差别. 所以这里以一个例子的形式来分析一下这2种类型转换的使用场景和差别. ...

  4. csrf攻击实例

    CSRF 攻击可以在受害者毫不知情的情况下以受害者名义伪造请求发送给受攻击站点,从而在并未授权的情况下执行在权限保护之下的操作.比如说,受害者 Bob 在银行有一笔存款,通过对银行的网站发送请求 ht ...

  5. Json 后台转对象的方式或者获取属性值方式。

    类似这类的 json字符串 后台转成 model 或者取其中一个属性值. 需要去掉前后引号 以及将转义字符去掉.空格代替 resoult = resoult.Substring(0, resoult. ...

  6. js浮点数乘除法

    JS在处理浮点数计算时经常会遇到精度的问题,上一篇博客封装了JS浮点数加减法的方法,这一次来封装一下js浮点数乘除法运算. 其实浮点除法的封装跟加减法的封装原理是一样,只是在第一次计算完后会再复位小数 ...

  7. Jquery 如何获取表单中的全部元素的值

    1.使用var formData = $(formId).serialize()获取:获取数据的格式为url参数形式的字符串.例如:id=100&name=张三   2.服务器端使用parse ...

  8. UI设计中蕴涵着系统重要的数据结构与功能设计

    UI设计中蕴涵着系统重要的数据结构与功能设计 UI设计中的用户需求,事件(用例)驱动

  9. 数据库之游标过程-- 基于MySQL

    实例如下: DROP PROCEDURE IF EXISTS pr_change_station_user_acct_his; -- 如果存在存储过程,即删除存储过程 create procedure ...

  10. COGS 147. [USACO Jan08] 架设电话线

    ★★☆   输入文件:phoneline.in   输出文件:phoneline.out   简单对比时间限制:1 s   内存限制:16 MB Farmer John打算将电话线引到自己的农场,但电 ...