这个从二维数组中检索值的算法的时间复杂度是多少?
What is the time complexity of this algorithm which retrieves values from a 2D array?
这是从 2D array
中检索值的算法,这个算法的 time complexity
是什么?
for(i=0;i<n;i++){
for(j=0;j<2;j++){
x = arr[i][j]
}
}
所以这个算法的time complexity
会是O(2n)
还是O(n^2)
? reason
是什么?
外层循环迭代n次,而内层循环只迭代两次,所以时间复杂度是O(2n)而不是O(n^2)。并且
O(2n) = O(n)
这是从 2D array
中检索值的算法,这个算法的 time complexity
是什么?
for(i=0;i<n;i++){
for(j=0;j<2;j++){
x = arr[i][j]
}
}
所以这个算法的time complexity
会是O(2n)
还是O(n^2)
? reason
是什么?
外层循环迭代n次,而内层循环只迭代两次,所以时间复杂度是O(2n)而不是O(n^2)。并且
O(2n) = O(n)