以下代码的时间复杂度..?
我对以下代码的时间复杂度感到困惑......
i = 0
//first row
if(board[i][0] == win && board[i][1] == win && board[i][2] == win)
return win;
//second row
if(board[i+1][0] == win && board[i+1][1] == win && board[i+1][2] == win)
return win;
//third row
if(board[i+2][0] == win && board[i+2][1] == win && board[i+2][2] == win)
return win;
//first col
if(board[0][i] == win && board[1][i] == win && board[1][i] == win)
return win;
//second col
if(board[0][i+1] == win && board[1][i+1] == win && board[2][i+1] == win)
return win;
//third col
if(board[0][i+2] == win && board[1][i+2] == win && board[2][i+2] == win)
return win;
//first diag
if(board[i][i] == win && board[i+1][i+1] == win && board[i+2][i+2] == win)
return win;
//second diag
if(board[i+2][i] == win && board[i+1][i+1] == win && board[i][i+2] == win)
return win;
I am confused with the time complexity of the following piece of code....
i = 0
//first row
if(board[i][0] == win && board[i][1] == win && board[i][2] == win)
return win;
//second row
if(board[i+1][0] == win && board[i+1][1] == win && board[i+1][2] == win)
return win;
//third row
if(board[i+2][0] == win && board[i+2][1] == win && board[i+2][2] == win)
return win;
//first col
if(board[0][i] == win && board[1][i] == win && board[1][i] == win)
return win;
//second col
if(board[0][i+1] == win && board[1][i+1] == win && board[2][i+1] == win)
return win;
//third col
if(board[0][i+2] == win && board[1][i+2] == win && board[2][i+2] == win)
return win;
//first diag
if(board[i][i] == win && board[i+1][i+1] == win && board[i+2][i+2] == win)
return win;
//second diag
if(board[i+2][i] == win && board[i+1][i+1] == win && board[i][i+2] == win)
return win;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
假设 board[M][N] 是一个二维数组,它将以恒定时间运行,即 O(1)。
It will run in constant time i.e O(1) assuming board[M][N] to be a two dimensional array.
这显然是一道陷阱题,看你是否理解时间复杂度的概念。
时间复杂度衡量算法在应用于越来越大的输入时所需的数量级。您的示例确实仅依赖于恒定数量的输入,这就是为什么其他人正确地说 O(1) 的原因。从本质上讲,这意味着时间复杂度并不是衡量其效率、质量等的正确工具。
This is obviously a trap question to see if you understood the concept of time complexity.
Time complexity measures the order of magnitude that an algorithm needs if applied to larger and larger input. Your example does depend only on a constant number of inputs, this why the others correctly said O(1). In essence this means that time complexity is not the correct tool to measure its efficiency, quality or whatsoever.
O(1) - 没有迭代,也没有递归。
O(1) - no iterations nor recursion.
正如其他答案所暗示的,它是 O(1)。但这并不被认为是良好的编码实践。您可以使用循环来概括它。
As the other answers suggest, it is O(1). But this is not considered as good coding practice. You can use a loop to generalize it.
正如您在此处所示的那样,它的复杂度为 O(1),因为它没有可变方面。执行总是需要相同的时间。
如果将其放入 i 从 0 到 n-1 的循环中,则它将具有 O(n),即线性复杂度。如果将 n 的大小加倍,则执行时间大约也会加倍。
As you've shown it there, it is O(1) because there are no variable facets to it. It will always take the same time to execute.
If you put it in a loop where i goes from 0 to n-1, then it will have O(n), i.e. linear complexity. If you double the size of n, you approximately double the execution time.