【发布时间】:2022-01-10 10:45:05
【问题描述】:
我想知道以下代码的时间复杂度是否为 O(n)(平均时间或编码面试的上下文):
for i in len(range(ls)):
Dict = {}
......
define foo
if foo not in Dict.keys():
Dict[foo] = 0
【问题讨论】:
-
在 Python 3 上是
O(n),在 Python 2 上是O(n**2)(其中.keys()返回一个list,它们是O(n)来进行遏制测试,而不是O(1)dict)。删除.keys(),两者都是O(n)。
标签: python dictionary hash hashmap