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

Update (2015-06-11):
The class name of the Java function had been updated to MyStack instead of Stack.

Credits:
Special thanks to @jianchao.li.fighter for adding this problem and all test cases.

由于是队列,后进后出,栈顶元素在队列的尾部,结合一个flag判断栈顶在哪个队列。一开始两个队列都为空的时候,随便插入其中一个,比如q1,将q1的标志设为1,q2这时为空。如果要出栈,则将q1最后元素之前的所有元素出列,加入q2,并将标志设为q2,q1最后一个元素出列,此时q1为空.当要插入时,选择两者中为空的那个队列插入,并设标志为新插入的那个队列。

实现写了70+行,还需要优化

class Stack {
public:
// Push element x onto stack.
void push(int x) {
if (flag) //栈顶在队列1,继续加入
q1.push(x);
else
q2.push(x);
} // Removes the element on top of the stack.
void pop() {
if (!empty()) {
if (flag) {//栈顶在队列1,将队列1转移到队列2
while (!q1.empty()) {
int temp = q1.front();
q1.pop();
if (q1.empty()) //弹出的是最后一个元素
flag = false; //栈顶在队列2
else //尚不是最后一个,则加入队列2
q2.push(temp);
}
}
else { //栈顶在队列2
while (!q2.empty()) {
int temp = q2.front();
q2.pop();
if (q2.empty()) //弹出的是最后一个元素
flag = true; //栈顶在队列1
else //尚不是最后一个,则加入队列1
q1.push(temp);
}
}
}
} // Get the top element.
int top() {
int res = ;
if (flag) {//栈顶在队列1,将队列1转移到队列2
while () {
res = q1.front();
q1.pop();
if (q1.empty()) {//弹出的是最后一个元素
q1.push(res);//再压回去
break;
}
else //尚不是最后一个,则加入队列2
q2.push(res);
}
}
else { //栈顶在队列2
while () {
res = q2.front();
q2.pop();
if (q2.empty()) {//弹出的是最后一个元素
q2.push(res);
break;
}
else //尚不是最后一个,则加入队列1
q1.push(res);
}
}
return res;
} // Return whether the stack is empty.
bool empty() {
return q1.empty()&&q2.empty();
}
queue<int> q1,q2;
bool flag = true; //true表示栈顶在队列1,false表示栈顶在队列2
};

其他博客的实现

http://blog.csdn.net/xy010902100449/article/details/49307823

http://blog.csdn.net/sunao2002002/article/details/46482673

Leetcode 225 两个队列实现栈的更多相关文章

  1. LeetCode 225:用队列实现栈 Implement Stack using Queues

    题目: 使用队列实现栈的下列操作: push(x) -- 元素 x 入栈 pop() -- 移除栈顶元素 top() -- 获取栈顶元素 empty() -- 返回栈是否为空 Implement th ...

  2. LeetCode 225题用队列实现栈(Implement Stack using Queues) Java语言求解

    链接 https://leetcode-cn.com/problems/implement-stack-using-queues/ 思路 首先演示push()操作:将元素依次进入队1,进入时用top元 ...

  3. 两个栈实现队列+两个队列实现栈----java

                                               两个栈实现队列+两个队列实现栈----java 一.两个栈实现一个队列 思路:所有元素进stack1,然后所有出s ...

  4. Algorithm --> 两个栈实现队列和两个队列实现栈

    两个栈实现队列和两个队列实现栈 队列(queue)先进先出的线性表:栈(stack)先进后出的线性表. 两个栈实现队列 法一思路: s1是入栈的,s2是出栈的. 入队列:直接压入s1即可: 出队列:如 ...

  5. 两个队列实现栈&两个栈实现队列(JAVA)

    1,两个栈实现队列 题目描述 用两个栈来实现一个队列,完成队列的Push和Pop操作. 队列中的元素为int类型. 思路:栈的特点时先进后出,队列的特点是先进先出. 若此时有两个队列stack1,st ...

  6. 用两个栈实现队列与用两个队列实现栈(Python实现)

    用两个栈实现队列: class QueueWithTwoStacks(object): def __init__(self): self._stack1 = [] self._stack2 = [] ...

  7. 剑指offer第二版面试题9:用两个队列实现栈(JAVA版)

    题目:用两个队列实现栈. 分析:通过一系列的栈的压入和弹出操作来分析用队列模拟一个栈的过程,如图所示,我们先往栈内压入一个元素a.由于两个队列现在都是空,我们可以选择把a插入两个队列中的任一个.我们不 ...

  8. 编程题目: 两个队列实现栈(Python)

    感觉两个队列实现栈 比 两个栈实现队列 麻烦 1.栈为空:当两个队列都为空的时候,栈为空 2.入栈操作:当队列2为空的时候,将元素入队到队列1:当队列1位空的时候,将元素入队到队列2: 如果队列1 和 ...

  9. (LeetCode)两个队列来实现一个栈

    原题例如以下: Implement the following operations of a stack using queues. push(x) -- Push element x onto s ...

随机推荐

  1. ASP.NET MVC4 新手入门教程之五 ---5.用控制器访问模型数据

    在本节中,将创建一个新的MoviesController类并编写代码来检索电影数据并将其显示在浏览器中使用一个视图模板. 才走出下一步生成应用程序. 用鼠标右键单击控制器文件夹中并创建一个新的 Mov ...

  2. RabbitMQ - 任务队列

    这次我们试着实现这样一个小程序: 嗯,就是任务队列(task queue).不是将任务集中在一堆并一直等到所有任务一并完成为止,而是将每一个任务封装为一个消息,并将其发送到队列,后台的workers就 ...

  3. Java - USC2字符串截取

    Java内部采用UTF-16(USC2)编码,比如:"我" 为 98 17,"a" 为 0 97," " 为 0 32,"1&qu ...

  4. 【转】spring boot使用Druid和监控配置

    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/u012100371/article/details/76602612 Druid是Java语言中最好 ...

  5. 设计模式入门,装饰着模式,c++代码实现

    // test03.cpp : Defines the entry point for the console application.////设计模式第3章 装饰者模式#include " ...

  6. MVC 控制器中直接访问url 的方式

    public void ShowDetailsImg() { //生成MD5码 string path = @"D:\其他\Test\WebApplication2\WebApplicati ...

  7. mysql if函数使用例子

    1.场景一 有时查询数量a 同时查询过滤后的数量b 2. 代码 SELECT count(id) as total_count, count( IF ( date(order_time) = DATE ...

  8. 软件项目技术点(9)——如何将gif动态图拆分绘制

    AxeSlide软件项目梳理   canvas绘图系列知识点整理 背景介绍 我们的软件支持插入gif图片,并且展示在软件里是动态的,例如插入下面这张gif图. 在软件里显示的同样是这样的动态效果: 那 ...

  9. html标签篇(2)

    上次讲到<a>标签,并没有细说a标签用法. <!DOCTYPE html> <html lang="en"> <head>  < ...

  10. VS code 自定义快捷输入

    本文是从简书复制的, markdown语法可能有些出入, 想看"正版"和更多内容请关注 简书: 小贤笔记 位置 ctrl+shift+p 搜索: snippets 输入类型: 比如 ...