【每日阅读】2021年04月26日-如何计算算法时间复杂度

int cal(int n) {
   int sum = 0;
   int i = 1;
   for (; i <= n; ++i) {
     sum = sum + i;
   }
   return sum;
 }

这段代码总的执行时间是 (2n+2)*unit_time,取影响最大的部分,时间复杂度即是n。

int cal(int n) {
   int sum = 0;
   int i = 1;
   int j = 1;
   for (; i <= n; ++i) {
     j = 1;
     for (; j <= n; ++j) {
       sum = sum +  i * j;
     }
   }
 }

这段代码总的执行时间 T(n) = (2n平方+2n+3)*unit_time,取影响最大的部分,时间复杂度即是n平方

原创文章,作者:geekgao,如若转载,请注明出处:https://www.geekgao.cn/archives/2975

(0)
geekgaogeekgao博主
上一篇 2021年4月26日
下一篇 2021年4月28日

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

GitHub
分享本页
返回顶部

Warning: error_log(/usr/local/lighthouse/softwares/wordpress/wp-content/plugins/spider-analyser/#log/log-2701.txt): failed to open stream: No such file or directory in /usr/local/lighthouse/softwares/wordpress/wp-content/plugins/spider-analyser/spider.class.php on line 2900