2024年最全【算法leetcode】剑指 Offer II 041(4)


分析

  • 面对这道算法题目,二当家的陷入了沉思。
  • 计算平均值仅需要总值和数量,但是当数字的数量超过滑动窗口的大小时,就需要将前面的数字移除,先进先出,用队列实现最直观。

题解

rust

struct MovingAverage {
    size: usize,
    sum: i32,
    q: std::collections::VecDeque<i32>,
}


/\*\*
 \* `&self` means the method takes an immutable reference.
 \* If you need a mutable reference, change it to `&mut self` instead.
 \*/
impl MovingAverage {

    /\*\* Initialize your data structure here. \*/
    fn new(size: i32) -> Self {
        MovingAverage{
            size: size as usize,
            sum: 0,
            q: std::collections::VecDeque::new()
        }
    }
    
    fn next(&mut self, val: i32) -> f64 {
        if self.q.len() == self.size {
            self.sum -= self.q.pop\_front().unwrap();
        }
        self.q.push\_back(val);
        self.sum += val;
        self.sum as f64 / self.q.len() as f64
    }
}

/\*\*
 \* Your MovingAverage object will be instantiated and called as such:
 \* let obj = MovingAverage::new(size);
 \* let ret\_1: f64 = obj.next(val);
 \*/


go

type MovingAverage struct {
    size int
	sum  int
	q    []int
}


/\*\* Initialize your data structure here. \*/
func Constructor(size int) MovingAverage {
    return MovingAverage{size: size}
}


func (this \*MovingAverage) Next(val int) float64 {
    if len(this.q) == this.size {
		this.sum -= this.q[0]
		this.q = this.q[1:]
	}
	this.sum += val
	this.q = append(this.q, val)
	return float64(this.sum) / float64(len(this.q))
}


/\*\*
 \* Your MovingAverage object will be instantiated and called as such:
 \* obj := Constructor(size);
 \* param\_1 := obj.Next(val);
 \*/


c++

class MovingAverage {
private:
    int size;
    int sum;
    queue<int> q;
public:
    /\*\* Initialize your data structure here. \*/
    MovingAverage(int size) {
        this->sum = 0;
        this->size = size;
    }
    
    double next(int val) {
        if (this->q.size() == this->size) {
            this->sum -= this->q.front();
            this->q.pop();
        }
        this->sum += val;
        this->q.emplace(val);
        return this->sum \* 1.0 / this->q.size();
    }
};

/\*\*
 \* Your MovingAverage object will be instantiated and called as such:
 \* MovingAverage\* obj = new MovingAverage(size);
 \* double param\_1 = obj->next(val);
 \*/


java

class MovingAverage {
    private final int size;
    private int sum;
    private final Queue<Integer> q = new LinkedList<>();

    /\*\* Initialize your data structure here. \*/
    public MovingAverage(int size) {
        this.size = size;
    }

    public double next(int val) {
        if (q.size() == size) {
            sum -= q.poll();
        }
        sum += val;
        q.offer(val);
        return sum \* 1.0 / q.size();
    }
}

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


python

class MovingAverage:

    def \_\_init\_\_(self, size: int):
        """


![img](https://img-blog.csdnimg.cn/img_convert/5d4f1f3a3c617325f2281b91cf7badb6.png)
![img](https://img-blog.csdnimg.cn/img_convert/75117844d64869adfe6e4b61262667d1.png)

**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**

**[需要这份系统化资料的朋友,可以戳这里获取](https://bbs.csdn.net/topics/618545628)**


**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**

题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**

**[需要这份系统化资料的朋友,可以戳这里获取](https://bbs.csdn.net/topics/618545628)**


**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值