Java AC solution using queue


  • 0
    U
    public class MovingAverage {
    LinkedList<Integer> queue;
    int ws;// widow size
    /** Initialize your data structure here. */
    public MovingAverage(int size) {
        queue= new LinkedList<Integer>();
        ws=size;
        
    }
    
    public double next(int val) {
        double res=0;
        queue.add(val);
        if(queue.size()>ws)
            queue.poll();
        for(int i=0;i<queue.size();i++)
            res +=queue.get(i);
        return res/queue.size();
            
        
    }
    

    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.