我对下面一段代码的时间复杂性感到困惑…
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;
最佳答案
这显然是一个陷阱问题,看你是否理解时间复杂性的概念。
时间复杂性测量如果应用到越来越大的输入,算法需要的数量级。你的例子只依赖于固定数量的输入,这就是为什么其他人正确地说O(1)从本质上讲,这意味着时间复杂度不是衡量其效率、质量或其他方面的正确工具。
关于c++ - 以下代码的时间复杂度。,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5414775/