简体   繁体   English

不是严格平方的二维数组的时间复杂度是多少?

[英]What it's the time complexity of 2D array not strictly square?

Let's assume we have a 2D array that looks like this假设我们有一个看起来像这样的二维数组

array = [[1,2,3],
         [4,5,6]] 

As you can see, it's not a square 2D array, and we have a function that sums all the values in it.如您所见,它不是一个方形二维数组,我们有一个 function 将其中的所有值相加。

def sum_values(array):
     total = 0
     for i in array:
         for j in i:
             total += j
     return total
      

So the question is, is the time complexity still O(n^2)?那么问题来了,时间复杂度还是O(n^2)吗? or something like O(ab) or something else?或像O(ab)或其他东西?

you can define n as the size of the input matrix and say the algorithm is O(n) or you can define n as the number of rows and m as the number of columns in the input and say the algorithm is O(n*m)您可以将n定义为输入矩阵的大小并说算法为O(n)或者您可以将n定义为行数和m为输入中的列数并说算法为O(n*m)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM