题目浅析

  • 想查看原题可以点击题目链接

  • 简单地说,就是自己实现一个类,能够存储数字(给的数字按升序排列),同时每次插入数字都会返回已有数字中数值上最近 3000 的个数。

思路分享

代码解答(强烈建议自行解答后再看)

  • 参考题解
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class RecentCounter:
def __init__(self):
self.que = deque()

def ping(self, t: int) -> int:
self.que.append(t)
while self.que[-1] - self.que[0] > 3000:
# self.que = self.que[1:]
self.que.popleft()
return len(self.que)


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