【问题标题】:Can Anyone Tell me the Time Complexity of these scenario? [duplicate]谁能告诉我这些场景的时间复杂度? [复制]
【发布时间】:2021-04-12 03:57:21
【问题描述】:

这段C语言代码,我想知道这两个场景的时间复杂度?

a.)

    for(i=1; i<=n; i++)
       for(j=i; i<=n; j++)
          printf("Hi");

b.)

    for(i=1; i<=n; i*=3)
        for(j=1; i<=n; j++)
           printf("Hello");

【问题讨论】:

  • 两种情况都需要无限时间,因为内部循环不会检查 j

标签: algorithm loops iteration time-complexity dsa


【解决方案1】:

对于 que a,它将是 n^2,因为它由 2 个 for 循环组成,即 n+1(n)=n^2+1。

【讨论】:

    猜你喜欢
    • 2020-11-14
    • 2021-03-09
    • 2021-09-07
    • 1970-01-01
    • 1970-01-01
    • 2013-06-05
    • 2020-10-04
    • 2015-12-29
    • 2014-08-14
    相关资源
    最近更新 更多