算法

算法 - 队列类型

2021-02-28  本文已影响0人  羽晞yose

队列

队列的应用场景

JS异步中的任务队列

异步队列

计算最近请求次数

leeCode第933题

这题看到答案那么多人连题目都看不懂我就放心了。
条件就是第一个输入如果为ping,则将第二个输入当前时间毫秒数放进队列中。队列中小于当毫秒数的就出队列,当然题目假设全都是结果为ping(什么拍平啊,concat的全都不用),所以要做的就是将当前t - 3000毫秒外的踢出去,返回当前队列中的长度就行了,题目不难

const RecentCounter = function() {
    this.q = []
};

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

/**
 * Your RecentCounter object will be instantiated and called as such:
 * var obj = new RecentCounter()
 * var param_1 = obj.ping(t)
 */
上一篇 下一篇

猜你喜欢

热点阅读