编程作业二

作业链接: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.javaLinkedStack.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的更多相关文章

  1. Coursera Algorithms Programming Assignment 2: Deque and Randomized Queue (100分)

    作业原文:http://coursera.cs.princeton.edu/algs4/assignments/queues.html 这次作业与第一周作业相比,稍微简单一些.有三个编程练习:双端队列 ...

  2. Deques and Randomized Queues

    1. 题目重述 完成三个程序,分别是双向队列,随机队列,和随机队列读取文本并输出k个数. 2. 分析 2.1 双向队列 题目的性能要求是,操作时间O(1),内存占用最大48n+192byte. 当使用 ...

  3. 课程一(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 ...

  4. Algorithms: Design and Analysis, Part 1 - Programming Assignment #1

    自我总结: 1.编程的思维不够,虽然分析有哪些需要的函数,但是不能比较好的汇总整合 2.写代码能力,容易挫败感,经常有bug,很烦心,耐心不够好 题目: In this programming ass ...

  5. Algorithms : Programming Assignment 3: Pattern Recognition

    Programming Assignment 3: Pattern Recognition 1.题目重述 原题目:Programming Assignment 3: Pattern Recogniti ...

  6. Programming Assignment 2: Randomized Queues and Deques

    实现一个泛型的双端队列和随机化队列,用数组和链表的方式实现基本数据结构,主要介绍了泛型和迭代器. Dequeue. 实现一个双端队列,它是栈和队列的升级版,支持首尾两端的插入和删除.Deque的API ...

  7. AlgorithmsI PA2: Randomized Queues and Deques RandomizedQueue

    RandomizedQueue 有几个关键点: 1. 选择合适的数据结构,因为需要任意位置删除元素,Linked list 做不到,必须使用resizing arrays. 2. resizing 的 ...

  8. AlgorithmsI PA2: Randomized Queues and Deques Deque

    本次作业考察利用array 或者linked list 实现规定时间复杂度的queue 和stack, 不能使用java 自带的stack 和queue. 目的是让我们掌握自己设计的函数的复杂度. D ...

  9. AlgorithmsI PA2: Randomized Queues and Deques Subset

    本题的bonus是 因此方法是queue的size 达到了K, 就停止增加元素,保证queue.size() 最大时只有k. Java code: import edu.princeton.cs.al ...

随机推荐

  1. npm install 后缀

    npm 全局安装与本地安装 npm install express # 本地安装 npm install express -g # 全局安装 本地安装 将安装包放在 ./node_modules 下( ...

  2. oracle 报错归纳总结

    1.ORA-00904 正常情况下是找不到字段(大概率字段名字错了) 还有一种比较特别的情况实在设计DB的时候,字段小写,在sql查询工具自动提示的时候都是大写,造成字段找不到的情况.

  3. 关于SVN提交注释的问题

    如果客户端是TortoiseSVN的话,在客户端要设置的版本库上点右键,选择菜单TortoiseSVN--属性,新建属性,选择属性tsvn:logminsize,设置log的最短长度,然后提交.然后如 ...

  4. .net WINFORM的GDI双缓冲的实现

    有时候在窗体中执行不断的GDI+操作的时候会出现闪速的状况,除了修改窗体的参数,更应该解决刷新本身的问题,双缓冲可能就是这样来的. 方法1: 用GDI绘制在位图上,然后再重新生成位图 Bitmap b ...

  5. mac系统终端sudo免输入密码技能get

    1.需要在/etc/sudoers中配置. 这个文件的权限是r/r/n,配置之前需要加写权限. sudo chmod u-w /etc/sudoers 2.打开命令窗口sudo visudo 或者 s ...

  6. win32FTP程序设计

    掌握socket基于事件机制的网络程序设计,掌握多线程技术的FTP Server端设计方法,掌握FTP标准基本协议及其程序的实现,掌握文件内容的网络传输设计方法. 利用CFtpServer类接收和解析 ...

  7. KATANA Owin 资料收集

    https://www.cnblogs.com/xishuai/p/asp-net-5-owin-katana.html http://wiki.jikexueyuan.com/project/thi ...

  8. Android开发之旅2:HelloWorld项目的目录结构

    引言 前面Android开发之旅:环境搭建及HelloWorld,我们介绍了如何搭建Android开发环境及简单地建立一个HelloWorld项目,本篇将通过HelloWorld项目来介绍Androi ...

  9. Firbe Channel光纤信道

    简介 中文名:网状信道 外文名:Fibre Channel 简    称:FC 光纤信道是一种高速网络技术标准(T11),主要应用于SAN(存储局域网).其拓扑结构分为三种,点到点.仲裁循环.交换结构 ...

  10. [AMPPZ2014]Petrol

    关键点的最小生成树? 关键点初始化为0,跑多源最短路,然后重构整个图,用Kruskal跑最小生成树 然后跑树链剖分在线回答询问 对树上每个点维护到链顶的最大值,结合线段树可以做到\(\Theta(n ...