程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Leetcode solution (0933): number of recent requests (Python)

編輯:Python


subject :​ ​ Original link ​​( Simple )

solution

Time complexity

Spatial complexity

Execution time

Ans 1 (Python)

344ms (74.43%)

Ans 2 (Python)

376ms (42.68%)

Ans 3 (Python)


LeetCode Of Python Execution time , As long as there is no significant difference in time complexity , The execution time is generally of the same order of magnitude , For reference only .


Solution 1 ( Use arrays to implement ):

class RecentCounter:


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

def ping(self, t: int) -> int:
self.queue.append(t)
while self.queue[0] < t - 3000:
self.queue.pop(0)
return len(self.queue)
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.

Solution 2 ( Use collections.deque Realization )

class RecentCounter:


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

def ping(self, t: int) -> int:
self.queue.append(t)
while self.queue[0] < t - 3000:
self.queue.popleft()
return len(self.queue)
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.




  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved