吐槽一句,这题说实话题目看着很难理解。
一、队列
class RecentCounter(object):
def __init__(self):
self.pings = list()
def ping(self, t):
"""
:type t: int
:rtype: int
"""
self.pings.append(t)
while self.pings[0] < t - 3000:
self.pings.pop(0)
return len(self.pings)
# Your RecentCounter object will be instantiated and called as such:标题
# obj = RecentCounter()
# param_1 = obj.ping(t)
二、deque
from collections import deque
class RecentCounter(object):
def __init__(self):
self.pinglist = deque()
def ping(self, t):
"""
:type t: int
:rtype: int
"""
self.pinglist.append(t)
while self.pinglist[0]<t-3000:
self.pinglist.popleft()
return len(self.pinglist)
# Your RecentCounter object will be instantiated and called as such:
# obj = RecentCounter()
# param_1 = obj.ping(t)