时间复杂度常用案例
常量阶O(1)
num = 1 # 只执行一次
num = num + n # 只执行一次
print(num) # 只执行一次
对数阶O(logn)
i = 1
while i <= n:
i = i * 2
线性对数阶O(nlogn)
for j in range(n):
i = 1
while i <= n:
i *= 2
线性阶O(n)
sum = 0 # 只执行一次
for i in range(0, n): # 执行n次
count = count + 1 # 执行n次
平方阶O(n^2)
双重for循环
立方阶O(n^3)
三重for循环