给一个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. Computer architecture Computer organization

    COMPUTER ORGANIZATION AND ARCHITECTURE DESIGNING FOR PERFORMANCEComputer architectureNINTH EDITION C ...

  2. P1092 虫食算 NOIP2002

    为了测试stl 30分的暴力写法... #include <bits/stdc++.h> using namespace std; const int maxn = 11; int n; ...

  3. wajueji

    #include<stdio.h>int map[3]={42,3,99};int step[3]={0};int max=99999;void qian(){ int i=0; int ...

  4. oracle utf8字符集转gbk(转)

    近日有同事在外面部署系统时,安装数据库时可能选择了UTF-8编码格式,导入insert语句时,一个汉字被认为三个字节,这是不行的. 结合上网搜到的资料,将oracle数据库的编码格式,从utf-8改为 ...

  5. php的异步处理

    在PHP Web程序中,发送手机短信.电子邮件.转换视频格式.记录日志.数据挖掘采集等,都是比较耗时的操作. 为了增强用户体验,需要将这些操作转为异步执行 PHP Web程序中的短耗时异步处理   前 ...

  6. Redis学习笔记(5)-Set

    package cn.com; import java.util.HashMap; import java.util.Map; import java.util.Set; import redis.c ...

  7. C#中Attribute介绍

    什么是特性? MSDN中定义为:公共语言运行时运行添加类似关键字的描述声明,叫做Attribute,它对程序中的元素进行标注,如类型.方法.字段和属性等.attribute和Microsoft.Net ...

  8. node.js创建服务器报错

    创建nodeTest.js如下: var http = require('http'); http.createServer(function (request, response){ respons ...

  9. css3 loading效果

    file:///E:/zhangqiangWork/2014/SPDbank/index.html 参考该网站 http://tobiasahlin.com/spinkit/ 查看源代码把里面的dom ...

  10. XUtils框架中HttpUtils使用Get请求时总是返回相同信息的问题解决,xutilshttputils

    如需转载请标明出处:http://blog.csdn.net/itas109 版本:Xutils 2014年11月11日 下载地址:https://github.com/wyouflf/xUtils ...