成人无码视频,亚洲精品久久久久av无码,午夜精品久久久久久毛片,亚洲 中文字幕 日韩 无码

資訊專欄INFORMATION COLUMN

[LeetCode] 346. Moving Average from Data Stream

svtter / 1738人閱讀

Problem

Given a stream of integers and a window size, calculate the moving average of all integers in the sliding window.

Example:

MovingAverage m = new MovingAverage(3);
m.next(1) = 1
m.next(10) = (1 + 10) / 2
m.next(3) = (1 + 10 + 3) / 3
m.next(5) = (10 + 3 + 5) / 3

Solution
class MovingAverage {

    Queue queue;
    int size;
    int sum;
    /** Initialize your data structure here. */
    public MovingAverage(int size) {
        this.queue = new LinkedList<>();
        this.size = size;
        this.sum = 0;
    }
    
    public double next(int val) {
        if (queue.size() == size) {
            sum -= queue.poll();
        }
        queue.offer(val);
        sum += val;
        return (double) sum / queue.size();
    }
}

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

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://m.hztianpu.com/yun/77381.html

相關(guān)文章

  • 科學(xué)計算與數(shù)據(jù)可視化1

    摘要:科學(xué)計算與數(shù)據(jù)可視化程序設(shè)計模塊最重要的一個特點就是其維數(shù)組對象即該對象是一個快速而靈活的大數(shù)據(jù)集容器。兩行及以上為二維表示數(shù)組各維度大小的元組。 科學(xué)計算與數(shù)據(jù)可視化1 @(程序設(shè)計) numpy模塊 Numpy最重要的一個特點就是其N維數(shù)組對象(即ndarray)該對象是一個快速而靈活的大數(shù)據(jù)集容器。 使用Numpy,開發(fā)人員可以執(zhí)行以下操作: 1、數(shù)組的算數(shù)和邏輯運算。 2、...

    aervon 評論0 收藏0
  • [LintCode/LeetCode] Sliding Window Maximum/Median

    摘要:窗口前進,刪隊首元素保證隊列降序加入當(dāng)前元素下標從開始,每一次循環(huán)都將隊首元素加入結(jié)果數(shù)組 Sliding Window Maximum Problem Given an array of n integer with duplicate number, and a moving window(size k), move the window at each iteration fro...

    crelaber 評論0 收藏0
  • [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 the list is even, there is no middle value. So the median is the mean of the two middle value. Examp...

    suemi 評論0 收藏0
  • Node.js 指南(流中的背壓)

    摘要:在數(shù)據(jù)緩沖區(qū)已超過或?qū)懭腙犃挟?dāng)前正忙的任何情況下,將返回。當(dāng)返回值時,背壓系統(tǒng)啟動,它會暫停傳入的流發(fā)送任何數(shù)據(jù),并等待消費者再次準備就緒,清空數(shù)據(jù)緩沖區(qū)后,將發(fā)出事件并恢復(fù)傳入的數(shù)據(jù)流。 流中的背壓 在數(shù)據(jù)處理過程中會出現(xiàn)一個叫做背壓的常見問題,它描述了數(shù)據(jù)傳輸過程中緩沖區(qū)后面數(shù)據(jù)的累積,當(dāng)傳輸?shù)慕邮斩司哂袕?fù)雜的操作時,或者由于某種原因速度較慢時,來自傳入源的數(shù)據(jù)就有累積的趨勢,就像...

    Tony 評論0 收藏0

發(fā)表評論

0條評論

閱讀需要支付1元查看
<