擅长:python、mysql、java
<p>“in”运算符是O(N)。
while是O(返回值)。你知道吗</p>
<p>[编辑]让我详细说明一下:您的代码相当于</p>
<pre><code>j = 1
while True: # is O(the returned j)
if j in A: # is O(N), bec. has to go through the whole array
j += 1
else:
return j
</code></pre>
<p>所以在最坏的情况下,你的算法确实是O(N^2)。在最佳情况下(例如,如果1不在A中),算法仅为O(N)。你知道吗</p>
<p>如果A是集合(不是列表或数组.array),平均为O(N)<em></em>。
请参见此处的详细信息:<a href="https://wiki.python.org/moin/TimeComplexity" rel="nofollow noreferrer">https://wiki.python.org/moin/TimeComplexity</a>或<a href="https://stackoverflow.com/questions/13884177/complexity-of-in-operator-in-python">Complexity of *in* operator in Python</a>。你知道吗</p>