队列的特点
- 先进先出
https://link.juejin.cn/?target= 在JavaScript中使用数组来模拟队列
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLicmbw5SNwYzM0QmNlZGNjVjZ1EGOiJ2Y0gDM1QmM5MWNiNDZz8CX5d2bs92Yl1iclB3bsVmdlR2LcNWaw9CXt92Yu4GZjlGbh5yYjV3Lc9CX6MHc0RHaiojIsJye.png)
使用队列的场景
- JS异步中的任务队列
队列的典型习题
实现代码
var RecentCounter = function () {
this.arr = [];
this.result = [];
};
RecentCounter.prototype.ping = function (t) {
// 新ping的元素入队
this.arr.push(t);
while (this.arr[0] < t - 3000) {
this.arr.shift();
}
return this.arr.length;
};
作者:Always_positive
链接:https://juejin.cn/post/6948663928008212510
来源:稀土掘金
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。