Implement queue with two stack 用两个堆实现队列
在上国外某教授的algorithm课,课后有一个小quiz,问题如下
Implement a queue with two stacks so that each queue operations takes a constant amortized number of stack operations.
用两个堆实现一个队列,这个队列每次操作的消耗应该是堆操作的分期常量。
我们知道队列里的元素满足先进先出的原则,而堆里面的元素满足后进先出原则。那么我们可以这么来做:
如果我们把这个俩个堆分为inbox 和outbox
进队(enqueue)
我们把元素push放入inbox
出队(dequeue)
发现如果outbox是empty的,我们把inbox里面的元素pop拿出来并且push放入outbox中
发现outbox不为空,则就将outbox里面的元素pop返回拿出来
java代码如下
public class QueueWithTwoStack {private Stackinbox=new Stack();private Stackoutbox=new Stack(); public void enqueue(T item){ inbox.push(item); } public T dequeue(){ if(outbox.isEmpty()){ while(!inbox.isEmpty()){ outbox.push(inbox.pop()); } } return outbox.pop(); } public static void main(String args[]){ QueueWithTwoStacktest=new QueueWithTwoStack<>(); for (int i=0;i<5;i++){ test.enqueue(i); } for (int i=0;i<5;i++){ int j= test.dequeue(); System.out.println(j); } } /**output: * 0 * 1 * 2 * 3 * 4 */ }
Implement queue with two stack 用两个堆实现队列相关推荐
- [CareerCup] 3.5 Implement Queue using Two Stacks 使用两个栈来实现队列
3.5 Implement a MyQueue class which implements a queue using two stacks. LeetCode上的原题,请参见我之前的博客Imple ...
- 剑指offer第二版-9.用两个栈实现队列
描述:使用两个栈实现一个队列.队列中实现尾部插入和头部删除函数. 思路:stack1负责插入,stack2负责弹出,如果stack2为空了,将stack1的元素依次弹出并存放到stack2中,之后对s ...
- LeetCode刷题记录12——232. Implement Queue using Stacks(easy)
LeetCode刷题记录12--232. Implement Queue using Stacks(easy) 目录 LeetCode刷题记录12--232. Implement Queue usin ...
- LeetCode 232. Implement Queue using Stacks
题目: Implement the following operations of a queue using stacks. push(x) – Push element x to the back ...
- Lintcode: Implement Queue by Stacks 解题报告
Implement Queue by Stacks 原题链接 : http://lintcode.com/zh-cn/problem/implement-queue-by-stacks/# As th ...
- LeetCode 232. Implement Queue using Stacks--用2个栈来实现一个队列--C++解法
LeetCode 232. Implement Queue using Stacks–C++解法 LeetCode题解专栏:LeetCode题解 我做的所有的LeetCode的题目都放在这个专栏里,大 ...
- Queue+Stack(C++,标准库中的队列和栈)
STL Queue(先进先出) 头文件#include 成员函数 queue::pop () //出队列 queue::push () //入队列 queue::size () //返回队列中元素个数 ...
- leetcode python3 简单题232. Implement Queue using Stacks
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第二百三十二题 (1)题目 英文: Implement the following ...
- 如何使用两个堆栈实现队列_使用两个队列实现堆栈
如何使用两个堆栈实现队列 Stack and Queue at a glance... 堆叠和排队一目了然... Stack: 堆栈: The stack is an ordered list whe ...
- 剑指Offer #05 用两个栈实现队列(模拟)
题目来源:牛客网-剑指Offer专题 题目地址:用两个栈实现队列 题目描述 用两个栈来实现一个队列,完成队列的Push和Pop操作. 队列中的元素为int类型. 题目解析 首先,我们需要知道一下基本知 ...
最新文章
- Windows下 安装Oracle Java 11 并设置环境变量
- python使用缩进作为语法边界-python二级备考 day2
- 解决Visual SVN在linux下不能访问的问题
- Redhat7 samba配置(部分)
- NetCat Tutorials
- vray学习笔记(3)-多维子材质是个什么东西
- python 修改图片尺寸_python 批量修改图片大小
- java编程规范每行代码窄字符,wiki/0xFE_编程规范.md at master · islibra/wiki · GitHub
- 往mysql数据库中存当前日期_在MySQL数据库中存储日期的最简单方法是什么?
- Centos6.6部署Redis集群
- Android中将一个图片切割成多个图片
- jdk AbstractStringBuilder实现
- #怎样获取当前时间和时区_JDK1.8新增日期时间类型
- 算法笔记练习 题解合集
- python键值对是什么意思_python键值对
- 大数据技术栈思维导图
- 实战 | Python爬取B站柯南弹幕+Gephi梳理主线剧情
- python-电脑时间校准
- 项目集管理PgMP:聚焦战略、收益与治理︱项目管理者联盟联合创始人宣晓锋
- 爬虫入门(三)——动态网页爬取:爬取pexel上的图片
热门文章
- springmvc 使用Jackson的配置
- [.net 面向对象编程基础] (6) 基础中的基础——运算符和表达式
- [BZOJ 3888] [Usaco2015 Jan] Stampede 【线段树】
- Javascript中eval函数的用法
- Java Futrue模式简单例子和使用场景
- JVM监控及诊断工具GUI篇之Visual VM
- Parallel垃圾回收器总结
- spring源码--第七个后置处理器的使用:初始化方法
- rem 产生的小数像素问题
- Mysql + keepalived 实现双主热备读写分离【转】