Programming Assignment 2: Deques and Randomized Queues
编程作业二
作业链接:Deques and Randomized Queues & Checklist
我的代码:Deque.java & RandomizedQueue.java & Permutation.java
问题简介
Write a generic data type for a deque and a randomized queue. The goal of this assignment is to implement elementary data structures using arrays and linked lists, and to introduce you to generics and iterators.
任务摘要
Dequeue. A double-ended queue or deque (pronounced “deck”) is a generalization of a stack and a queue that supports adding and removing items from either the front or the back of the data structure. Create a generic data type Deque that implements the following API:
public class Deque<Item> implements Iterable<Item> {
public Deque() // construct an empty deque
public boolean isEmpty() // is the deque empty?
public int size() // return the number of items on the deque
public void addFirst(Item item) // add the item to the front
public void addLast(Item item) // add the item to the end
public Item removeFirst() // remove and return the item from the front
public Item removeLast() // remove and return the item from the end
public Iterator<Item> iterator() // return an iterator over items in order from front to end
public static void main(String[] args) // unit testing (optional)
}
Randomized queue. A randomized queue is similar to a stack or queue, except that the item removed is chosen uniformly at random from items in the data structure. Create a generic data type RandomizedQueue that implements the following API:
public class RandomizedQueue<Item> implements Iterable<Item> {
public RandomizedQueue() // construct an empty randomized queue
public boolean isEmpty() // is the randomized queue empty?
public int size() // return the number of items on the randomized queue
public void enqueue(Item item) // add the item
public Item dequeue() // remove and return a random item
public Item sample() // return a random item (but do not remove it)
public Iterator<Item> iterator() // return an independent iterator over items in random order
public static void main(String[] args) // unit testing (optional)
}
Iterator. Each iterator must return the items in uniformly random order. The order of two or more iterators to the same randomized queue must be mutually independent; each iterator must maintain its own random order.
Client. Write a client program Permutation.java that takes an integer k as a command-line argument; reads in a sequence of strings from standard input using StdIn.readString(); and prints exactly k of them, uniformly at random. Print each item from the sequence at most once.
详细的要求参见:specification。
问题分析
第二次作业比较简单,主要想让我们用数组和链表实现基础的数据结构,介绍 Java 的泛型和迭代器,还可以参考 ResizingArrayStack.java 和 LinkedStack.java。
任务一要求实现双端队列,即在队列的首尾都可以进行插入和删除操作。因为性能要求里要每次操作都保证在常数时间内完成,所以选择链表实现。确定了实现方式,不行再照着课程样例,建议加的哨兵节点也没什么问题。
任务二要求实现随机队列,随机出队,每个位置出去的概率一样,迭代器返回的顺序也要是随机的。Checklist 里的提示降低了很多难度:
What is meant by uniformly at random?
If there are n items in the randomized queue, then you should choose each one with probability 1/n, up to the randomness of StdRandom.uniform(), independent of past decisions. You can generate a pseudo-random integer between 0 and n−1 using StdRandom.uniform(n) from StdRandom.
Given an array, how can I rearrange the entries in random order?
Use StdRandom.shuffle()—it implements the Knuth shuffle discussed in lecture and runs in linear time. Note that depending on your implementation, you may not need to call this method.
所以这个随机队列用数组实现就比较方便,随机数组下标好操作,也符合作业的目的,一个用链表,一个用数组。而且,任务二性能要求空间部分,也保证我们用变长数组才能拿到满分,总之,课程设计还是很厉害的。于是,任务二也没什么问题。
任务三的测试程序,要求从文件里读入 N 个字符串,然后随机输出 K 个。直接把 N 个字符串压入任务二的随机队列,然后输出 K 个就行。但是,还有个额外的挑战:
For an extra challenge, use only one Deque or RandomizedQueue object of maximum size at most k.
挑战不能直接压入 N 个,队列里最多只能放 K 个字符串,大概是这次作业最难的部分了。当初是没有想出来,找到这篇博客才拿到了额外的分数。
测试结果

填坑,继续小号提交原来通过的代码。
Programming Assignment 2: Deques and Randomized Queues的更多相关文章
- Coursera Algorithms Programming Assignment 2: Deque and Randomized Queue (100分)
作业原文:http://coursera.cs.princeton.edu/algs4/assignments/queues.html 这次作业与第一周作业相比,稍微简单一些.有三个编程练习:双端队列 ...
- Deques and Randomized Queues
1. 题目重述 完成三个程序,分别是双向队列,随机队列,和随机队列读取文本并输出k个数. 2. 分析 2.1 双向队列 题目的性能要求是,操作时间O(1),内存占用最大48n+192byte. 当使用 ...
- 课程一(Neural Networks and Deep Learning),第三周(Shallow neural networks)—— 3.Programming Assignment : Planar data classification with a hidden layer
Planar data classification with a hidden layer Welcome to the second programming exercise of the dee ...
- Algorithms: Design and Analysis, Part 1 - Programming Assignment #1
自我总结: 1.编程的思维不够,虽然分析有哪些需要的函数,但是不能比较好的汇总整合 2.写代码能力,容易挫败感,经常有bug,很烦心,耐心不够好 题目: In this programming ass ...
- Algorithms : Programming Assignment 3: Pattern Recognition
Programming Assignment 3: Pattern Recognition 1.题目重述 原题目:Programming Assignment 3: Pattern Recogniti ...
- Programming Assignment 2: Randomized Queues and Deques
实现一个泛型的双端队列和随机化队列,用数组和链表的方式实现基本数据结构,主要介绍了泛型和迭代器. Dequeue. 实现一个双端队列,它是栈和队列的升级版,支持首尾两端的插入和删除.Deque的API ...
- AlgorithmsI PA2: Randomized Queues and Deques RandomizedQueue
RandomizedQueue 有几个关键点: 1. 选择合适的数据结构,因为需要任意位置删除元素,Linked list 做不到,必须使用resizing arrays. 2. resizing 的 ...
- AlgorithmsI PA2: Randomized Queues and Deques Deque
本次作业考察利用array 或者linked list 实现规定时间复杂度的queue 和stack, 不能使用java 自带的stack 和queue. 目的是让我们掌握自己设计的函数的复杂度. D ...
- AlgorithmsI PA2: Randomized Queues and Deques Subset
本题的bonus是 因此方法是queue的size 达到了K, 就停止增加元素,保证queue.size() 最大时只有k. Java code: import edu.princeton.cs.al ...
随机推荐
- I/O模式总结
进程读取数据时要经过两个阶段: 1.等待内核准备数据: 2.将内核缓冲区中的数据复制到进程缓冲区中. 一.阻塞IO 进程会阻塞在等待内核准备数据和数据从内核空间复制到用户空间这两个阶段. 二.非阻塞I ...
- HDU 1863 畅通工程(Prim算法求解MST)
题目: 省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可).经过调查评估,得到的统计表中列出了有可能建设公路的若干条道路的成本.现 ...
- Qt5 编程基础
Qt 是一个C++ GUI应用框架,Qt 具有良好的可移植性支持大多数桌面和移动操作系统并常用于嵌入式开发. Qt的发行版分为商业版和开源版,提供了Qt Creator作为轻量级IDE. Hello ...
- 三种数据库访问——Spring JDBC
本篇随笔是上两篇的延续:三种数据库访问——原生JDBC:数据库连接池:Druid Spring的JDBC框架 Spring JDBC提供了一套JDBC抽象框架,用于简化JDBC开发. Spring主要 ...
- jsonp跨域简单应用(一)
转载:http://www.cnblogs.com/cyg17173/p/5865364.html ashx+jsonp+document.referrer -- 一年前学的JSONP 跨域,一年 ...
- Node.js学习笔记(八) --- Node.js的路由模块封装
1 .模块化的方式封装 整理中… 2 .封装仿照 express 的路由整理中…
- 设置TableView section header的颜色
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInte ...
- Java开发中常用的设计模式(三)---建造者模式
一. 模式结构 建造者模式主要包含四个角色: Product:产品角色. Builder:抽象建造者.它声明为创建一个Product对象的各个部件指定的抽象接口. ConcreteBuilder:具体 ...
- Unity之使用技巧记录
A:写了个死循环Unity无响应崩溃了怎么办? Q::到文件夹里找到刚刚写的脚本,把错误的代码屏蔽掉,再回到Unity
- input pattern中常用的正则表达式
常用的正则表达式 pattern的用法,只是列出来一些常用的正则: 信用卡 [0-9]{13,16} 银联卡 ^62[0-5]\d{13,16}$ Visa: ^4[0-9]{12}(?:[0-9]{ ...