这么写貌似不对

来源:4-3 LeetCode:933. 最近的请求次数

vZina

2021-05-29

[“RecentCounter”,“ping”,“ping”,“ping”,“ping”,“ping”]
[[],[1],[100],[3001],[3002],[6666]]

输出的是 [null,1,2,3,3,3]
预期的是 [null,1,2,3,3,1]

非要用队列,写个递归

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

/** 
 * @param {number} t
 * @return {number}
 */
RecentCounter.prototype.ping = function(t) {
    this.q.push(t);

    let shiftFn = () => {
        if(this.q[0] >= t - 3000) return;
        this.q.shift();
        shiftFn()
    }
   shiftFn();

    return this.q.length
};

/**
 * Your RecentCounter object will be instantiated and called as such:
 * var obj = new RecentCounter()
 * var param_1 = obj.ping(t)
 */
写回答

1回答

lewis

2021-05-29

你的问题是啥

0
3
龙晓秀
回复
vZina
你代码写错了吧 我按照老师代码写的 加了6666没问题哦
2023-09-28
共3条回复

JavaScript版数据结构与算法 轻松解决前端算法面试

夯实算法基础,填补技术短板,助力面试考题最后一公里

2479 学习 · 683 问题

查看课程