Skip to content

·栈是一个先进先出的数据结构

·JS中没有队列,但可以用Array实现队列的所有功能

·栈常用操作:push/shift/queue[queue.length-1]

使用场景

食堂排队打饭

JS异步中的任务队列

计算最近请求次数

计算最近请求次数(LC933)

输入: input = [[],[1],[100],[3001],[3002]]

输出: [null,1,2,3,3]

解题步骤:新请求入队,3000ms 前发出的请求出队,队列长度即最近请求次数

md
var RecentCounter = function(){
    this.q = []
}

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

md

More

Check out the documentation for the full list of runtime APIs.

最近更新