【问题标题】:What is the time complexity of this algorithm which retrieves values from a 2D array?这个从二维数组中检索值的算法的时间复杂度是多少?
【发布时间】:2021-05-22 08:29:51
【问题描述】:

这是从2D array 检索值的算法,该算法的time complexity 是什么?


for(i=0;i<n;i++){
 for(j=0;j<2;j++){
    x = arr[i][j]
  }
}

所以这个算法的time complexityO(2n) 还是O(n^2)reason 是什么?

【问题讨论】:

  • O(2n) 原因:内循环迭代2次
  • 请注意O(2n) = O(n)!

标签: c algorithm time-complexity big-o


【解决方案1】:

外循环迭代n次,而内循环只迭代两次,所以时间复杂度是O(2n)而不是O(n^2)。和

O(2n) = O(n)

【讨论】:

    猜你喜欢
    • 2015-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多