前言
有一个网友在知乎留言:
https://zhuanlan.zhihu.com/p/87754009
「关注SpringBoot公众号不迷路」
本文会采用进阶式的讲解方式,从一阶到三阶,在到3阶的变形。
对于时间复杂度是怎么回事,还不懂的话,请先阅读《烦不烦,别再问我时间复杂度了:这次不色,女孩子进来吧 - 第281篇》:
https://mp.weixin.qq.com/s/F1HLfWkYok4D633OpcLjwA
一、一阶的时间复杂度
一节的应该是非常的简单的,为了文章的完整性,我们还是简单的看下代码:
-
/**
-
* 一阶的例子
-
* @author 悟纤「公众号SpringBoot」
-
* @param n
-
*/
-
public static void test1(int n) {
-
int count = 0;
-
for(int i=1;i<=n;i++) {
-
count++;
-
}
-
System.out.println(count);
-
}
一层for循环,n等于多少就循环多少次,那么可以得知时间频度:
T(n)=n
那么时间复杂就是:O(n)
二、二阶的时间复杂度
我们看下二阶的小栗子:
-
/**
-
* 二阶的例子
-
* @author 悟纤「公众号SpringBoot」
-
* @param n
-
*/
-
public static void test2(int n) {
-
int count = 0;
-
for(int i=1;i<=n;i++) {
-
for(int j=1;j<=n;j++) {
-
count++;
-
}
-
}
-
System.out.println(count);
-
}
双层for循环的执行的次数就是n*n次。那么时间复杂度就是O(n²)
三、三阶的时间复杂度(立方阶)
。。。。。。。。。。。。。。。。。
版权原因,完整文章,请参考如下:立方阶时间复杂度怎么算-征战沙场- 第326篇