【问题标题】:Space and time complexity空间和时间复杂度
【发布时间】:2020-03-23 23:24:25
【问题描述】:
function compareStringLengths ( a, b )
{
  if ( a.length < b.length )
    return -1;
  if ( a.length > b.length )
    return 1;
  return 0;
}

var fruits = [ "apple", "pear", "orange", "banana" ];
fruits.sort ( compareStringLengths );

谁能告诉我这段代码的时间和空间复杂度?我猜时间复杂度是 O(n^2)。

【问题讨论】:

  • 是什么语言?

标签: sorting time compare complexity-theory space


【解决方案1】:

空间复杂度:线性,即 O(n)(因为您的所有数据都在数组中) 时间复杂度:O(n log(n))(因为排序)

【讨论】:

    猜你喜欢
    • 2012-08-14
    • 1970-01-01
    • 2013-09-12
    • 2016-02-13
    • 1970-01-01
    • 2014-04-29
    • 1970-01-01
    • 1970-01-01
    • 2021-01-23
    相关资源
    最近更新 更多