Coursera Algorithms week2 栈和队列 练习测验: Queue with two stacks
题目原文:
Implement a queue with two stacks so that each queue operations takes a constant amortized number of stack operations.
题目要求用栈实现队列的所有操作。
package week2; import java.util.Stack; /**
* Queue with two stacks. Implement a queue with two stacks so that each queue
* operations takes a constant amortized number of stack operations
* @author yangjingjing
*
*/
public class QueueWith2Stacks<E>{
Stack<E> inStack = new Stack<E>();
Stack<E> outStack = new Stack<E>();
public boolean isEmpty(){
if(inStack.isEmpty() && outStack.isEmpty())
return true;
else return false;
}
public int size(){
return (inStack.size() + outStack.size());
}
public void enqueue(E item){
inStack.push(item);
}
public E dequeue(){
if(outStack.isEmpty()){
if(inStack.isEmpty()) return null;
else{
while(!inStack.isEmpty()) {
outStack.push(inStack.pop());
}
return outStack.pop();
}
}else{
return outStack.pop();
}
}
public static void main(String[] args) {
QueueWith2Stacks<Object> queue = new QueueWith2Stacks<Object>();
queue.enqueue("a");
queue.enqueue("b");
queue.enqueue("c");
System.out.println(queue.dequeue());
queue.enqueue(1);
queue.enqueue(2);
Object o = null;
while((o = queue.dequeue()) != null) {
System.out.println(o);
}
}
}
Coursera Algorithms week2 栈和队列 练习测验: Queue with two stacks的更多相关文章
- Coursera Algorithms week2 栈和队列 练习测验: Stack with max
题目原文: Stack with max. Create a data structure that efficiently supports the stack operations (push a ...
- Coursera Algorithms Programming Assignment 2: Deque and Randomized Queue (100分)
作业原文:http://coursera.cs.princeton.edu/algs4/assignments/queues.html 这次作业与第一周作业相比,稍微简单一些.有三个编程练习:双端队列 ...
- LeetCode 232题用栈实现队列(Implement Queue using Stacks) Java语言求解
题目链接 https://leetcode-cn.com/problems/implement-queue-using-stacks/ 题目描述 使用栈实现队列的下列操作: push(x) -- 将一 ...
- C#LeetCode刷题之#232-用栈实现队列(Implement Queue using Stacks)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4108 访问. 使用栈实现队列的下列操作: push(x) -- ...
- Coursera Algorithms week2 基础排序 练习测验: Dutch national flag 荷兰国旗问题算法
第二周课程的Elementray Sorts部分练习测验Interview Questions的第3题荷兰国旗问题很有意思.题目的原文描述如下: Dutch national flag. Given ...
- Coursera Algorithms week2 基础排序 练习测验: Permutation
题目原文: Given two integer arrays of size n , design a subquadratic algorithm to determine whether one ...
- Coursera Algorithms week2 基础排序 练习测验: Intersection of two sets
题目原文: Given two arrays a[] and b[], each containing n distinct 2D points in the plane, design a subq ...
- Coursera Algorithms week4 基础标签表 练习测验:Inorder traversal with constant extra space
题目原文: Design an algorithm to perform an inorder traversal of a binary search tree using only a const ...
- Coursera Algorithms week4 基础标签表 练习测验:Check if a binary tree is a BST
题目原文: Given a binary tree where each
随机推荐
- WCF开发的流程-服务端和客户端之间的通讯(内含demo讲解)
讲解技术之前,恳请博友让我说几句废话.今天是我第一在博客园发布属于自己原创的博文(如有雷同,那是绝对不可能的事,嘿嘿).之前一直是拜读各位博友的大作,受益匪浅的我在这对博友们说声谢谢,谢谢你们的共享! ...
- Python标准库sys
1.命令行参数sys.argv 我们从Python语言之模块第一部分的例子开始,看看sys.argv中到底存了些什么内容. #Filename: using_sys.py import sys i=0 ...
- 在Windows下安装Elasticsearch5.0
1.准备工作 安装和配置Java环境 2.下载 地址:https://www.elastic.co/downloads/elasticsearch 老版本:https://www.elastic.co ...
- Sping——使用注解创建切面
为讲解例子,我们首先定义一个Performance接口: package aoptest; public interface Performance { public void perform(); ...
- ASP.NET Log4Net日志的配置及使用,文件写入
Log4net是Apache log4j框架在Microsort.NET平台实现的框架. 帮助程序员将日志信息输出到各种目标(控制台,数据库,文件等) 1.新建一个ASP.NET项目 2.新建一个 l ...
- api 签名算法
<?php define('token', 'tokensecret'); // 定义私钥token /** * 哈希验证签名 */ function hmacSign($array, $tok ...
- Django - ORM实现用户登陆
1.路由分发cmdb(app)下urls.py中,建立url与函数对应关系 2.login.html代码: 3.views.py中,login函数,确认是否登陆成功 备注:从前端 获取用户名,密码,在 ...
- 好用的JS数字格式化
/* *js格式化数字代码 * *value: 要格式化的数字值 *scale: 最多保留几位小数 *zeroed: 是否保留尾0 *percented: 是否转称百分比形式 * */ functio ...
- Win32 线程同步
Win32 线程同步 ## Win32线程同步 ### 1. 原子锁 ### 2. 临界区 {全局变量} CRITICAL_SECTION CS = {0}; // 定义并初始化临界区结构体变量 {线 ...
- 基于Composer的Laravel扩展包开发工作流
使用场景 在引用第三方包的时候,对第三方包有改动需求,需要将代码放在自己的仓库:并且自己的其他项目也有需求引用自定义的第三方包:甚至自己会发布修改后的第三方包: 读完本文你讲获得: Git Submo ...