关闭

346. Moving Average from Data Stream

8人阅读 评论(0) 收藏 举报
分类:

https://leetcode.com/problems/moving-average-from-data-stream/description/

题目大意:初始化一个滑动窗口,大小为w,输入一系列数,求窗口内的平均数,窗口会向前滑动,当窗口填满时,将最早进入的数弹出,加入新的数.
解题思路:用队列,求和时可以利用上次的和,不用每次从头到尾求
代码:

class MovingAverage {
public:
    /** Initialize your data structure here. */
    MovingAverage(int size) {
        q_size = size;
        sum = 0;
    }

    double next(int val) {
        double res;
        if (q.size() < q_size) {  //当队列未达上限
            q.push(val);
            sum += val;  //利用上次求和结果
        } else {  //当队列达上限
            //减头
            sum -= q.front();  
            q.pop();
            //加尾
            q.push(val);
            sum += val;  
        }
        res = (double)sum / q.size();
        return res;
    }
private:
    queue<int> q;  //队列.模拟滑动窗口
    int q_size;  //队列上限
    int sum;  //暂存求和
};

/**
 * Your MovingAverage object will be instantiated and called as such:
 * MovingAverage obj = new MovingAverage(size);
 * double param_1 = obj.next(val);
 */
0
0
查看评论

LeetCode 346. Moving Average from Data Stream(数据流移动平均值)

原题网址:https://leetcode.com/problems/moving-average-from-data-stream/ Given a stream of integers and a window size, calculate the moving average of al...
  • jmspan
  • jmspan
  • 2016-05-01 06:23
  • 1527

[leetcode] 346. Moving Average from Data Stream 解题报告

题目链接:https://leetcode.com/problems/moving-average-from-data-stream/ Given a stream of integers and a window size, calculate the moving average of...
  • qq508618087
  • qq508618087
  • 2016-06-15 13:15
  • 2106

346. Moving Average from Data Stream

Given a stream of integers and a window size, calculate the moving average of all integers in the sliding window. For example, MovingAverage m =...
  • zshouyi
  • zshouyi
  • 2017-02-11 09:15
  • 401

[LC]346. Moving Average from Data Stream

一、问题描述 Given a stream of integers and a window size, calculate the moving average of all integers in the sliding window. For example, Movin...
  • cos2cot
  • cos2cot
  • 2017-11-29 08:41
  • 13

LeetCode 346. Moving Average from Data Stream

Given a stream of integers and a window size, calculate the moving average of all integers in the sliding window. For example, MovingAverage m = n...
  • github_34333284
  • github_34333284
  • 2016-06-20 12:09
  • 227

Moving Average from Data Stream

一道设计题,有3点 1. 一开始,自己用的是queue来做,但是发现,若想每次peek下一个值,都得先poll,这才发现当中问题,改成了list来做,可以做到get(index),所以用queue和用queue的思想来解决问题是完全不同的做法。 2. 数值转换,如何从int转为double,看当中的...
  • sjphiChina
  • sjphiChina
  • 2016-06-06 18:35
  • 442

Leetcode 346. Moving Average from Data Stream (Easy) (cpp)

Leetcode 346. Moving Average from Data Stream (Easy) (cpp)
  • Niko_Ke
  • Niko_Ke
  • 2016-12-14 04:16
  • 209

Leetcode Everyday: 346. Moving Average from Data Stream

https://leetcode.com/problems/moving-average-from-data-stream/ Given a stream of integers and a window size, calculate the moving average of all inte...
  • jimhust
  • jimhust
  • 2016-05-11 20:53
  • 104

LeetCode 346. Moving Average from Data Stream(java)

Given a stream of integers and a window size, calculate the moving average of all integers in the sliding window.For example, MovingAverage m = new Mo...
  • katrina95
  • katrina95
  • 2018-02-03 06:56
  • 5

[Leetcode] 346. Moving Average from Data Stream 解题报告

题目: Given a stream of integers and a window size, calculate the moving average of all integers in the sliding window. For example, MovingAv...
  • magicbean2
  • magicbean2
  • 2017-08-11 16:05
  • 153
    个人资料
    • 访问:48519次
    • 积分:1751
    • 等级:
    • 排名:千里之外
    • 原创:121篇
    • 转载:29篇
    • 译文:0篇
    • 评论:2条
    文章分类
    最新评论