Time complexity of nested for-loop

前端 未结 6 766
花落未央
花落未央 2020-11-22 08:51

I need to calculate the time complexity of the following code:

for (i = 1; i <= n; i++)
{
  for(j = 1; j <= i; j++)
  {
   // Some code
  }
}
         


        
6条回答
  •  猫巷女王i
    2020-11-22 09:12

    Indeed, it is O(n^2). See also a very similar example with the same runtime here.

提交回复
热议问题