剑指 Offer II 042. 最近请求次数

101 阅读1分钟

剑指 Offer II 042. 最近请求次数

一个 RecentCounter 类来计算特定时间范围内最近的请求。

var RecentCounter = function () {
  this.times = [];
};

RecentCounter.prototype.ping = function (t) {
  this.times.push(t);
  while (this.times[0] < t - 3000) {
    this.times.shift();
  }
  return this.times.length;
};