Concise C++ queue solution


  • 0
    M

    class MovingAverage {
    public:
    /** Initialize your data structure here. */
    MovingAverage(int size) {
    n = size;
    sum = 0;
    }

    double next(int val) {
        sum += val;
        q.push_back(val);
        if(q.size() > n) {
            sum -= q.front();
            q.pop_front();
        }
        return sum / double(q.size());
        
    }
    int n, sum;
    deque<int> q;
    

    };


Log in to reply
 

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