小编yyy*_*yyy的帖子

嵌套for循环的时间复杂度

我需要计算以下代码的时间复杂度:

for (i = 1; i <= n; i++)
{
  for(j = 1; j <= i; j++)
  {
   // Some code
  }
}
Run Code Online (Sandbox Code Playgroud)

O(n ^ 2)

complexity-theory big-o time-complexity

30
推荐指数
4
解决办法
8万
查看次数

复杂性计算

什么是复杂性:

int f4(int n)
{
   int i, j, k=1, count = 0;

   for(i = 0; i < n; i++) 
   {
      k *= 3;

      for(j = k; j; j /= 2)
         count++;
   }

   return count;
}
Run Code Online (Sandbox Code Playgroud)

我知道它是O(n ^ 2)但你怎么计算这个?为什么不是n*log n?

complexity-theory

9
推荐指数
1
解决办法
2482
查看次数

标签 统计

complexity-theory ×2

big-o ×1

time-complexity ×1