Number of recent requests

Returns the number of times the last request time was less than 3000 milliseconds.

The problem solving code

If the request time of the queue header exceeds 3000, the queue header will leave the queue, and then the queue length will be returned.

var RecentCounter = function() {
  this.queue = new Array(a);// Create an array mock queue
};

/ * * *@param {number} t
 * @return {number}* /
RecentCounter.prototype.ping = function(t) {
  this.queue.push(t);
  while (t - this.queue[0] > 3000) { // Determine whether the time difference between the first request in the queue and the current request exceeds 3000.
    this.queue.shift();
  }
  return this.queue.length; // Return the queue length
};
Copy the code