一、时间复杂度和空间复杂度分析
1、时间复杂度
(1)、如何理解时间复杂度的表示法
如何理解算法时间复杂度的表示法,例如 O(n²)、O(n)、O(1)、O(nlogn) 等?www.zhihu.com(2)、时间复杂度的分类
时间复杂度表达,经常用Big O notation来代表 (只看最高复杂度的运算)
- O(1) : constant complexity 常数复杂度 [时间复杂度为常数/O(1)的时间复杂度]
- O (log n): logarithmic complexity 对数复杂度 [O (log n)时间复杂度]
- O(n): linear complexity 线性时间复杂度
- O(n^3): N cubic complexity 立方
- O(n^2): N square complexity 平方
- O(2^n): exponential growth 指数
- O(n!): factorial 阶乘
前面的常数系数是不考虑的,比如O(1)并不代表他的复杂度就一定是1,也有可能是2,3,...
(3)、时间复杂度
直接看这段代码,根据n的不同情况它会运行多少次
O(1):
int n=1000;
System.out.printfIn("Hey: "+n);
int n=1000;
System.out.printfIn("He