2022-01-20
2022-01-20 本文已影响0人
16孙一凡通工
Go写队列可以用数组直接代替
Go版本:
type MovingAverage struct {
size int
stack []int
sum int
}
/** Initialize your data structure here. */
func Constructor(size int) MovingAverage {
return MovingAverage{size:size,stack:[]int{},sum:0}
}
func (this *MovingAverage) Next(val int) float64 {
this.sum=0;
this.stack=append(this.stack,val)
data_size:=len(this.stack);
if data_size>this.size{
this.stack=this.stack[1:data_size]
}
data_size=len(this.stack);
for _,value:=range this.stack{
this.sum+=value;
}
return float64(this.sum)/float64(data_size);
}