首页 > 其他分享 >【队列】LeetCode 346. 数据流中的移动平均值

【队列】LeetCode 346. 数据流中的移动平均值

时间:2023-01-22 16:44:24浏览次数:60  
标签:currentSum val int private queue 346 数据流 LeetCode size

题目链接

346. 数据流中的移动平均值

思路

队列设计基础题

代码

class MovingAverage {
    private Integer currentSum = 0;
    private Queue<Integer> queue = new LinkedList<>();
    private int size;

    public MovingAverage(int size) {
        this.size = size;
    }

    public double next(int val) {
        queue.offer(val);
        currentSum += val;

        if(queue.size() > this.size){
            currentSum -= queue.remove();
        }

        return 1.0 * currentSum / queue.size();
    }
}

标签:currentSum,val,int,private,queue,346,数据流,LeetCode,size
From: https://www.cnblogs.com/shixuanliu/p/17064494.html

相关文章

  • [LeetCode] 684. Redundant Connection
    Inthisproblem,atreeisan undirectedgraph thatisconnectedandhasnocycles.Youaregivenagraphthatstartedasatreewith n nodeslabeledfrom......
  • leetcode笔记——328周赛
    1.二维前缀和,二维差分304.二维区域和检索-矩阵不可变-力扣(LeetCode)二维前缀和怎么处理,注意加减的下标 2.2537.统计好子数组的数目-力扣(LeetCode)首先看到子数......
  • 【双指针】LeetCode 409. 最长回文串
    题目链接409.最长回文串思路遍历字符串过程中统计字符出现个数,如果达到2则说明可以放到回文串的两端,需要result+=2。遍历完之后的回文串如果长度小于s,说明s中存......
  • LeetCode.541 反转字符串II
    1.题目给定一个字符串s和一个整数k,从字符串开头算起,每计数至2k个字符,就反转这2k字符中的前k个字符。如果剩余字符少于k个,则将剩余字符全部反转。如果剩余字符小......
  • LeetCode.面试题02.05-链表求和-题解分析
    题目来源面试题02.05.链表求和题目详情给定两个用链表表示的整数,每个节点包含一个数位。这些数位是反向存放的,也就是个位排在链表首部。编写函数对这两个整数求和,并......
  • [LeetCode] 1824. Minimum Sideway Jumps
    Thereisa 3laneroad oflength n thatconsistsof n+1 points labeledfrom 0 to n.Afrog starts atpoint 0 inthe second lane andwantsto......
  • LeetCode.383 赎金信
    1.题目给你两个字符串:ransomNote和magazine,判断ransomNote能不能由magazine里面的字符构成。如果可以,返回true;否则返回false。magazine中的每个字符只能在rans......
  • 【LeetCode链表#12】链表相交
    链表相交同:160.链表相交力扣题目链接(opensnewwindow)给你两个单链表的头节点headA和headB,请你找出并返回两个单链表相交的起始节点。如果两个链表没有交点,返回n......
  • LeetCode——刷题笔记二
         ......
  • LeetCode.202 快乐数
    1.题目编写一个算法来判断一个数n是不是快乐数。「快乐数」 定义为:对于一个正整数,每一次将该数替换为它每个位置上的数字的平方和。然后重复这个过程直到这个数变为1,也......