【问题标题】:What is the time complexity of my moveElementToEnd code?我的 moveElementToEnd 代码的时间复杂度是多少?
【发布时间】:2020-05-06 21:19:35
【问题描述】:
def moveElementToEnd(array, toMove):
    # Write your code here.
    for i in array:
        if i == toMove:
            array.append(array.pop(array.index(i)))
    return array

内置函数array.pop和array.index的时间复杂度是多少?

【问题讨论】:

    标签: arrays algorithm sorting


    【解决方案1】:

    O(N^2)

    在最坏的情况下,您必须为每个匹配的元素(可能全部)复制整个数组(如果 pop 有一些糟糕的优化,则复制两次)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-01-14
      • 1970-01-01
      • 1970-01-01
      • 2023-02-26
      • 2021-12-26
      • 2017-01-29
      相关资源
      最近更新 更多