亚洲中字慕日产2020,大陆极品少妇内射AAAAAA,无码av大香线蕉伊人久久,久久精品国产亚洲av麻豆网站

資訊專欄INFORMATION COLUMN

[LeetCode] 346. Moving Average from Data Stream

svtter / 1736人閱讀

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)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://www.ezyhdfw.cn/yun/77381.html

相關(guān)文章

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

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

    aervon 評(píng)論0 收藏0
  • [LintCode/LeetCode] Sliding Window Maximum/Median

    摘要:窗口前進(jìn),刪隊(duì)首元素保證隊(duì)列降序加入當(dāng)前元素下標(biāo)從開(kāi)始,每一次循環(huán)都將隊(duì)首元素加入結(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 評(píng)論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 評(píng)論0 收藏0
  • Node.js 指南(流中的背壓)

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

    Tony 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<