【问题标题】:Big O notation for n/2 in while-loop循环中 n/2 的大 O 表示法
【发布时间】:2021-03-13 15:04:13
【问题描述】:

我是数据结构的新手。

我想问一个问题,我们如何确定这个过程的Big-O符号值:

while(n%2==0){
   console.log(2);
   n=n/2;
}

什么是大 O 符号?之前谢谢。

【问题讨论】:

    标签: data-structures time-complexity big-o complexity-theory


    【解决方案1】:

    如果n 为奇数,则不执行循环。如果n 是偶数,那么它需要log2n base 2 的日志)迭代直到循环停止。它是log2n,因为n 在每次循环迭代中减半(i.e.,n=n/2;)。

    假设console.log(2); 花费c 时间,则整体复杂度将为O(logn)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-10
      • 1970-01-01
      • 2016-04-27
      • 1970-01-01
      • 2020-12-29
      • 2020-05-29
      相关资源
      最近更新 更多