开始使用免费开始使用

练习 Big O 表示法

在本练习中,您将继续巩固对 Big O 表示法的理解。

第 1 步,您将编写一个算法,打印以下列表中的所有元素:

colors = ['green', 'yellow', 'blue', 'pink']

该算法的复杂度应为 $O(n)$。

第 2 步和第 3 步,您将计算两个算法的时间复杂度。

本练习是课程的一部分

Python 中的数据结构与算法

查看课程

交互式实操练习

通过完成这段示例代码来试试这个练习。

colors = ['green', 'yellow', 'blue', 'pink']

def linear(colors):
  # Iterate the elements of the list
  ____:
    # Print the current element of the list
    print(____)	

linear(colors)
编辑并运行代码