AlgorithmsI PA2: Randomized Queues and Deques Subset
本题的bonus是
因此方法是queue的size 达到了K, 就停止增加元素,保证queue.size() 最大时只有k.
Java code:
- import edu.princeton.cs.algs4.StdIn;
- import edu.princeton.cs.algs4.StdOut;
- public class Subset {
- public static void main(String[] args){
- int k = Integer.parseInt(args[0]);
- RandomizedQueue<String> queue = new RandomizedQueue<String>();
- while(StdIn.hasNextLine() && !StdIn.isEmpty()) {
- if(queue.size() < k) {
- queue.enqueue(StdIn.readString());
- }else {
- break;
- }
- }
- for(int i = 0; i< k; i++) {
- StdOut.println(queue.dequeue());
- }
- }
- }
AlgorithmsI PA2: Randomized Queues and Deques Subset的更多相关文章
- 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 ...
- Programming Assignment 2: Randomized Queues and Deques
实现一个泛型的双端队列和随机化队列,用数组和链表的方式实现基本数据结构,主要介绍了泛型和迭代器. Dequeue. 实现一个双端队列,它是栈和队列的升级版,支持首尾两端的插入和删除.Deque的API ...
- Programming Assignment 2: Deques and Randomized Queues
编程作业二 作业链接:Deques and Randomized Queues & Checklist 我的代码:Deque.java & RandomizedQueue.java & ...
- Deques and Randomized Queues
1. 题目重述 完成三个程序,分别是双向队列,随机队列,和随机队列读取文本并输出k个数. 2. 分析 2.1 双向队列 题目的性能要求是,操作时间O(1),内存占用最大48n+192byte. 当使用 ...
- The Swiss Army Knife of Data Structures … in C#
"I worked up a full implementation as well but I decided that it was too complicated to post in ...
- Java 性能调优指南之 Java 集合概览
[编者按]本文作者为拥有十年金融软件开发经验的 Mikhail Vorontsov,文章主要概览了所有标准 Java 集合类型.文章系国内 ITOM 管理平台 OneAPM 编译呈现,以下为正文: 本 ...
- Coursera Algorithms Programming Assignment 2: Deque and Randomized Queue (100分)
作业原文:http://coursera.cs.princeton.edu/algs4/assignments/queues.html 这次作业与第一周作业相比,稍微简单一些.有三个编程练习:双端队列 ...
- Multi-processor having shared memory, private cache memories, and invalidate queues having valid bits and flush bits for serializing transactions
Multi-processor systems are often implemented using a common system bus as the communication mechani ...
随机推荐
- iOS-系统定位功能
ios系统定位 前期准备 系统定位功能,需要用到框架:CoreLocation/CoreLocation.h, 然后导入文件#import <CoreLocation/CoreLocation. ...
- 9.27 noip模拟试题
工资 (money/money.in/money.out) 时限1000ms 内存256MB 聪哥在暑假参加了打零工的活动,这个活动分为n个工作日,每个工作日的工资为Vi.有m个结算工钱的时间,聪哥可 ...
- mvc form
当点击提交按钮后,想在Controll里取到Form里的数据. 必须在控件上设置name属性 例如<input type='text',name='userId'/>, 在controll ...
- Session 原理
Session天天用,但是你真的理解了么? 今天遇到了这个问题,于是研究了一下.要解决这个问题,首先就要明白一些Session的机理.Session在服务器是以散列表形式存在的,我们都知道Sessio ...
- kissy
<!DOCTYPE HTML> <html lang="en-US"> <head> <meta charset="UTF-8& ...
- 什么是html,什么是php
学了这么长时间的网站建设,好像对这两个概念说以来还是语无伦次的,所以就来写一写了.html是什么呢,官方解释:超文本标记语言,超文本就是指页面可以包含图片,连接等非文字元素.超文本标记语言也是一种规范 ...
- Python - BeautifulSoup 安装
BeautifulSoup 3.x 1. 下载 BeautifulSoup. [huey@huey-K42JE python]$ wget http://www.crummy.com/software ...
- c#正则表达式采集数据
protected void Page_Load(object sender, EventArgs e){ StringBuilder MyStringBuilder = new StringBuil ...
- app包中的fragment和v4包中的fragment的使用的区别
app包中的fragment和v4包中的fragment的使用的区别 1.尽量不要用app包中的fragment,因为这个是在3.0之后才有的,支持的版本太高,在低版本中是是用不了的 2.androi ...
- 推荐Asp.net WebApi入门教程
Web API 强势入门指南; Web API 入门指南 - 闲话安全; 实例快速上手 -ASP.NET 4.5新特性WebAPI从入门到精通; Asp.net WebApi 项目示例(增删改查).