number-of-recent-calls

简述

求3000ms前到现在的ping数。
number-of-recent-calls 英文 中文

收获

1.用list来理解collections.deque()
2.https://docs.python.org/3/library/collections.html
3.廖雪峰collections

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class RecentCounter:

def __init__(self):
self.q = collections.deque()

def ping(self, t):
"""
:type t: int
:rtype: int
"""
self.q.append(t)
while self.q[0] < t-3000:
self.q.popleft()
return len(self.q)



# Your RecentCounter object will be instantiated and called as such:
# obj = RecentCounter()
# param_1 = obj.ping(t)
文章目录
  1. 简述
  2. 收获
  3. 代码
|