编程作业二

作业链接: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. Spark2.3.1中用各种模式来跑官方Demo

    1  使用单机local模式提交任务 local模式也就是本地模式,也就是在本地机器上单机执行程序.使用这个模式的话,并不需要启动Hadoop集群,也不需要启动Spark集群,只要有一台机器上安装了J ...

  2. Uploadify火狐出现302错误

    $(function () { var auth = "@(Request.Cookies[FormsAuthentication.FormsCookieName] == null ? st ...

  3. C学习笔记(1)-结构体、预处理与多文件结构程序设计

    一.结构体的定义与使用 #include <stdio.h> //定义结构体(类似模板) typedef struct { char name[50]; int age; char add ...

  4. 最长公共子序列(DP)

    Description 一个给定序列的子序列是在该序列中删去若干元素后得到的序列.确切地说,若给定序列 X = { x1,x2,…,xm },则另一序列Z ={ z1,z2,…,zk },X 的子序列 ...

  5. 第一次搭建dns服务器

    CentOS 7 搭建DNS服务器 主要参考的是小左先森的一篇博客:https://blog.51cto.com/13525470/2054121. 1.搭建过程中遇到的几个问题说一下: a.在重启服 ...

  6. mysql 修改初始化密码【转】

    转:http://www.jb51.net/article/39454.htm 方法1: 用SET PASSWORD命令 首先登录MySQL. 格式:mysql> set password fo ...

  7. [LeetCode]Flatten Binary Tree to Linked List题解(二叉树)

    Flatten Binary Tree to Linked List: Given a binary tree, flatten it to a linked list in-place. For e ...

  8. properties配置文件编码问题

    properties配置文件编码问题 person.last-name=哈哈 person.age=18 person.bitrh=2019/01/12 person.boss=false perso ...

  9. 算法 - 排序数组中与x最近一点

    条件: a[j] + a[j+1] < x*2 int findClosestPoint(int x,int a []) { int res = 0; int j = 0; while(j< ...

  10. 微软官方公布的Windows 8.1 Update常用快捷键

    以前用 Windows Server 2008R2,初装Win8.1,感觉最明显的是开关机速度真心快~下面摘录了常用的几个快捷键: Windows 键+D:显示或隐藏桌面 Windows键+X:访问Q ...