题目

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. nginx一个简单的反向代理设置

    location /aaaaa/ { proxy_pass http://localhost:8080/aaaaa/; } 经过配置,现在访问 http://localhost/aaaaa/   就会 ...

  2. 《java学习二》jvm性能优化-----认识jvm

    Java内存结构 Java堆(Java Heap) java堆是java虚拟机所管理的内存中最大的一块,是被所有线程共享的一块内存区域. 在虚拟机启动时创建.此内存区域的唯一目的就是存放对象实例,这一 ...

  3. ef 操作 mysql 中文乱码问题

    1.保证mysql数据的编码为utf8 启动mysql mysql -hlocalhost -uroot -p 输入密码 show VARIABLES like 'character_%'; SET  ...

  4. 关于下载文件封装的两个类(Mars)

    首先是文件FileUtils.java package mars.utils; import java.io.File; import java.io.FileOutputStream; import ...

  5. Redis string(字符串)

    1.getset key newValue   //给key设置value,并返回旧的value,如果没有旧的value,返回nil. 示例: getset age      //age 的值被设置为 ...

  6. COGS 1215. [Tyvj Aug11] 冗余电网

    ★   输入文件:ugrid.in   输出文件:ugrid.out   简单对比时间限制:1 s   内存限制:128 MB TYVJ八月月赛提高组第2题 测试点数目:5 测试点分值:20 --内存 ...

  7. SpringMVC Logback 设置及使用

    http://b6ec263c.wiz03.com/share/s/2SX2oY0nX4f32CY5ax1bapaL1WPGHe1OeQ-J2ijprB04A67k

  8. MyBatis归纳

      SqlSessionTemplate详解 SqlSessionTemplate类是MyBatis-Spring的核心.这个类负责管理MyBatis的SqlSession,调用MyBatis的SQL ...

  9. hd - MFM/IDE 硬盘设备

    描述 DESCRIPTION hd* 开头的设备是以裸模式(raw mode)访问MFM/IDE类型硬盘的块设备. 第一个IDE驱动控制器上的主盘(主设备号3)是 hda ;从盘是 hdb. 第二个I ...

  10. 如何下载js类库

    https://bower.io/  这个已经淘汰 https://learn.jquery.com/jquery-ui/environments/bower/ Web sites are made ...