简体   繁体   English

检查双端队列为空时的时间复杂度

[英]Time complexity when checking deque is empty

When I want to check whether deque is empty in python, I usually use this code snippet.当我想检查 python 中的双端队列是否为空时,我通常使用这个代码片段。

from collections import deque

q = deque()

# iterate until deque is empty
while q:
   do something...

I wonder how the time complexity is when checking whether the deque is empty.我想知道检查双端队列是否为空时的时间复杂度如何。 Does it take O(n) ?需要O(n)吗?

Is there any faster way to check whether deque is empty?有没有更快的方法来检查双端队列是否为空?

No there is not.不,那里没有。 while q is essentially while len(q) , and the time complexity of deque.__len__ is O(1). while q本质上是while len(q) ,而deque.__len__的时间复杂度是 O(1)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM