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

发布于 2024-10-26 04:25:19 字数 1069 浏览 1 评论 0原文

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

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

前事休说 2024-11-02 04:25:19

假设 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.

不气馁 2024-11-02 04:25:19

这显然是一道陷阱题,看你是否理解时间复杂度的概念。

时间复杂度衡量算法在应用于越来越大的输入时所需的数量级。您的示例确实仅依赖于恒定数量的输入,这就是为什么其他人正确地说 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.

樱娆 2024-11-02 04:25:19

O(1) - 没有迭代,也没有递归。

O(1) - no iterations nor recursion.

最单纯的乌龟 2024-11-02 04:25:19

正如其他答案所暗示的,它是 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.

喜爱皱眉﹌ 2024-11-02 04:25:19

正如您在此处所示的那样,它的复杂度为 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文