933. Number of Recent Calls

933. Number of Recent Calls

写一个RecentCounter类来计算最近的请求。

它只有一个方法:ping(int t),其中,t代表以毫秒为单位的某个时间。

返回从3000毫秒前到现在的ping数。

任何处于 [t-3000,t] 时间范围之内的ping都会被计算在内,包括当前(指t时刻)的ping。

保证每次对ping的调用都使用比之前更大的t值。

示例:

输入:inputs = ["RecentCounter","ping","ping","ping","ping"], inputs = [[],[1],[100],[3001],[3002]]
输出:[null,1,2,3,3]
import collections

class RecentCounter(object):
    def __init__(self):
        self.q = collections.deque()

    def ping(self, t):
        self.q.append(t)
        while self.q[0] < t-3000:
            self.q.popleft()
        return len(self.q)
原文地址:https://www.cnblogs.com/mrjoker-lzh/p/10593136.html