给一个datastream和一个fixed window size, 让我design一个class可以完成add number还有find average in the window. 就是不能用vector得用array. 

当然用queue是最好的

package DataStreamAverage;

import java.util.*;

public class Solution {
int count;
int sum;
Queue<Integer> q; public Solution() {
this.count = 0;
this.sum = 0;
this.q = new LinkedList<Integer>();
} public ArrayList<Double> average (HashSet<Integer> set, int L) {
ArrayList<Double> result = new ArrayList<Double>();
Iterator<Integer> iter = set.iterator();
while (iter.hasNext()) {
int cur = iter.next();
q.offer(cur);
count++;
sum += cur;
if (q.size()>L) {
int front = q.peek();
sum -= front;
count--;
q.poll();
}
double aver = (double)sum/count;
result.add(aver);
}
return result; } /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
HashSet<Integer> set = new HashSet<Integer>();
for (int i=1; i<=10; i++) {
set.add(i);
}
Solution sol = new Solution();
ArrayList<Double> res = sol.average(set, 2);
System.out.println(res);
} }

当然用fixed size array也可以,但是要小心,一个是要折回,二个是要删除数组元素之前存在的数

 package DataStreamAverage;

 import java.util.*;

 public class Solution2 {
int count;
int sum;
int[] arr;
int pos; public Solution2(int size) {
this.count = 0;
this.sum = 0;
this.arr = new int[size];
Arrays.fill(arr, Integer.MIN_VALUE);
this.pos = 0;
} public ArrayList<Double> average (HashSet<Integer> set, int L) {
ArrayList<Double> result = new ArrayList<Double>();
Iterator<Integer> iter = set.iterator();
while (iter.hasNext()) {
int cur = iter.next();
if (pos == arr.length) {
pos = 0;
}
if (arr[pos] != Integer.MIN_VALUE) {
sum -= arr[pos];
count--;
}
arr[pos] = cur;
sum += arr[pos];
pos++;
count++;
double aver = (double)sum/count;
result.add(aver);
}
return result;
} /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
HashSet<Integer> set = new HashSet<Integer>();
for (int i=1; i<=10; i++) {
set.add(i);
}
Solution2 sol = new Solution2(2);
ArrayList<Double> res = sol.average(set, 2);
System.out.println(res);
} }

G面经prepare: Data Stream Average的更多相关文章

  1. 346. Moving Average from Data Stream

    /* * 346. Moving Average from Data Stream * 2016-7-11 by Mingyang * 这里注意的就是(double) sum / count * su ...

  2. [LeetCode] Data Stream as Disjoint Intervals 分离区间的数据流

    Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers seen ...

  3. [LeetCode] Find Median from Data Stream 找出数据流的中位数

    Median is the middle value in an ordered integer list. If the size of the list is even, there is no ...

  4. Find Median from Data Stream

    常规方法 超时 class MedianFinder { vector<int> coll; public: MedianFinder(){ } void heapfu(vector< ...

  5. 数据结构与算法(1)支线任务8——Find Median from Data Stream

    题目如下:(https://leetcode.com/problems/find-median-from-data-stream/) Median is the middle value in an ...

  6. [LeetCode] Find Median from Data Stream

    Find Median from Data Stream Median is the middle value in an ordered integer list. If the size of t ...

  7. LeetCode——Find Median from Data Stream

    Median is the middle value in an ordered integer list. If the size of the list is even, there is no ...

  8. Leetcode: Data Stream as Disjoint Intervals && Summary of TreeMap

    Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers seen ...

  9. 295. Find Median from Data Stream

    题目: Median is the middle value in an ordered integer list. If the size of the list is even, there is ...

随机推荐

  1. Tomcat安装、启动和配置

    Tomcat服务器介绍 Tomcat是一个免费开发源代码的web应用服务器,具有与IIS.Apache等web服务器一样处理html页面的功能,另外它还是一个Servlet和JSP容器,独立的serv ...

  2. 自定义UserProvider,更改验证方法

    新建UserProvider,如果继承EloquentUserProvider,注入model是必须的,或者去继承interface,自己实现一些方法 use Illuminate\Auth\Eloq ...

  3. phpspec安装配置

    安装  composer require phpspec/phpspec -dev 运行  bin/phpspec 在laravel中  vendor/bin/phpspec 配置phpspec.ym ...

  4. Apache Kafka源码分析 - kafka controller

    前面已经分析过kafka server的启动过程,以及server所能处理的所有的request,即KafkaApis 剩下的,其实关键就是controller,以及partition和replica ...

  5. Android之Fragment学习笔记①

    Android Fragment完全解析,关于碎片你所需知道的一切 一. 什么是FragmentFragment(碎片)就是小型的Activity,它是在Android3.0时出现的.Fragment ...

  6. phpcms标签整理_当前栏目调用

    phpcms标签整理_当前栏目调用 转载 **//SQL语句调用: {pc:get sql="select * from phpcms_category where catid in($ca ...

  7. X Window 程式设计

    X Window 程式设计 转   http://www.cppblog.com/zmj/archive/2007/05/18/24331.html X Window 程式设计 X Window 程式 ...

  8. android source compiler

  9. Qt 之 设置窗口边框的圆角(使用QSS和PaintEvent两种方法)

    Qt在设置窗口边框圆角时有两种方式,一种是设置样式,另一种是在paintEvent事件中绘制窗口.下面分别叙述用这两种方式来实现窗口边框圆角的效果. 一.使用setStyleSheet方法 this- ...

  10. java简单优化和编写规范,自己总结的。

    1.永远不要比较两个浮点数是否相等.它是不安全的.详情google. 2.尽量使用StringBuffer代替String. 3.final类会提高很多效率. 4.try-catch 不应该用来控制程 ...