小编Rap*_*sen的帖子

时间复杂度:O(n)VS O(2 ^ n)

给定以下两个函数,为什么第一个n的时间复杂度为什么第二个为2 ^ n?

唯一的区别是第二个函数返回之前的+1。我不知道这如何影响时间复杂度。

int f1(int n){

   if (n==1)
    return 1;

  return f1(f1(n-1));


}

int f2(int n){

   if (n==1)
    return 1;

  return 1+f2(f2(n-1));


}

Run Code Online (Sandbox Code Playgroud)

time complexity-theory time-complexity

5
推荐指数
1
解决办法
176
查看次数

标签 统计

complexity-theory ×1

time ×1

time-complexity ×1