0
点赞
收藏
分享

微信扫一扫

每日一题-933. 最近的请求次数_Python

color_小浣熊 2022-05-06 阅读 45
leetcode
  • 写一个 RecentCounter 类来计算特定时间范围内最近的请求。

请你实现 RecentCounter 类:

  1. RecentCounter() 初始化计数器,请求数为 0 。
  2. int ping(int t) 在时间 t 添加一个新请求,其中 t 表示以毫秒为单位的某个时间,并返回过去 3000 毫秒内发生的所有请求数(包括新请求)。确切地说,返回在 [t-3000, t] 内发生的请求数。
  3. 保证 每次对 ping 的调用都使用比之前更大的 t 值。

示例 1:

提示:

程序代码

class RecentCounter:

    def __init__(self):
        self.requests = []


    def ping(self, t: int) -> int:
        self.requests.append(t)
        t1 = t - 3000
        return len([i for i in self.requests if i >= t1 and i <= t])


# Your RecentCounter object will be instantiated and called as such:
# obj = RecentCounter()
# param_1 = obj.ping(t)
举报

相关推荐

0 条评论