0
点赞
收藏
分享

微信扫一扫

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

梅梅的时光 2022-04-13 阅读 57

在这里插入图片描述

c++实现

class RecentCounter {
private:
    int slot;
    queue<int> time;
public:
    RecentCounter() {
        slot = 3000;
    }
    
    int ping(int t) {
        time.push(t);
        int early = t-slot;
        while (time.front()<early){
            time.pop();
        }
        return time.size();
    }
};

golang实现

type RecentCounter struct {
    slot int
    time []int
}

func Constructor() RecentCounter {
    return RecentCounter{
        slot : 3000,
        time : []int{}}
}

func (this *RecentCounter) Ping(t int) int {
    this.time =append(this.time,t)
    early := t-this.slot
    for this.time[0] < early {
        this.time = this.time[1:]
    }
    return len(this.time)
}
举报

相关推荐

0 条评论