空间复杂度、时间复杂度、递归



//空间复杂度:实现算法需要的额外的辅助空间

//时间复杂度:①只保留高阶项,低阶项直接丢弃
//                    ②系数不要
//                    ③执行次数是常数的是 O(1)


//递归需要有:①递归前进段   ②边界条件


/*
//时间复杂度 O(n)
//空间复杂度 O(n)  因为在Age的递归中,来回共是2n,但舍掉系数后为n   (类似的递归的时间复杂度需注意)
int Age(int n)
{
 int tmp;
 if(n == 1)
 {
  return 10;
 }
 else
  tmp = Age(n-1) + 2;
 return tmp;
}


//已知第一个人10岁,后一个人比前一个人大两岁,求第五个人的年龄?
#include <stdio.h>
int Age(int n)
{
 int tmp;
 if(n == 1)   //边界条件
 {
  return 10;
 }
 else
  tmp = Age(n-1) + 2;   //递归前进段
 return tmp;
}
int main()
{
 printf("%d\n",Age(5));
 return 0;
}
*/






猜你喜欢

转载自blog.csdn.net/bian1cheng1/article/details/80210732