常用网站:
算法可视化动画演示
https://www.cs.usfca.edu/~galles/visualization/Algorithms.html
常见的几种时间复杂度
O(x) | 英文释义 | 中文释义 |
---|---|---|
O(1) | Constant Complexity | 常数复杂度 |
O(log n) | Logarithmic Complexity | 对数复杂度 |
O(n) | Linear Complexity | 线性时间复杂度 |
O(n^2) | N square Complexity | 平方D |
O(n^3) | N cubic Complexity | 立方 |
O(2^n) | Exponential Growth | 指数 |
O(n!) | Factorial | 阶乘 |
注意:
1. 只看最高复杂度的运算。
2. O(x)前面的常数系数被选择性忽略,比如O(n)时间复杂度,可以运行2n次
时间复杂度曲线
递归状态的时间复杂度分析
例题:求斐波那契数列
分析思路:以fib(6)为例:
共有2^6次计算
改进方法:
a. 添加一个缓存机制
b. 使用循环
主定理
空间复杂度
- 代码中开了数组,则数组长度基本上就是空间复杂度
- 递归深度
参考文档:
- 如何理解算法时间复杂度的表示法,例如 O(n²)、O(n)、O(1)、O(nlogn) 等? https://www.zhihu.com/question/21387264
- https://en.wikipedia.org/wiki/Master_theorem_(analysis_of_algorithms)
- https://zh.wikipedia.org/wiki/主定理