美文网首页
2022-01-20

2022-01-20

作者: 16孙一凡通工 | 来源:发表于2022-01-20 20:02 被阅读0次

    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);
    
    
    }
    
    

    相关文章

      网友评论

          本文标题:2022-01-20

          本文链接:https://www.haomeiwen.com/subject/imojhrtx.html