gpt4 book ai didi

c++ - 以下代码的时间复杂度..?

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:28:51 26 4
gpt4 key购买 nike

我对以下代码的时间复杂度感到困惑....

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),假设 board[M][N] 是一个二维数组。

关于c++ - 以下代码的时间复杂度..?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5414775/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com