当前位置:网站首页>01-复杂度
01-复杂度
2022-06-12 18:02:00 【咸鱼~翻身】
1.什么是算法
- 算法是用于解决特定问题的一系列的执行步骤
2.要和评判一个算法的好坏
- 正确性、可读性、健壮性
- 时间复杂度:估算程序指令的执行次数
- 空间复杂度:估算所需占用的存储空间
3.大O表示法
- 大O表示法仅仅是一种粗略的分析模型
常见的复杂度
执行次数 | 复杂度 | 非正式术语 |
---|---|---|
12 | O(1) | 常数阶 |
2n+3 | O(n) | 线性阶 |
4n^2+2n+6 | O(n2) | 平方阶 |
4log2 n+25 | O(logn) | 对数阶 |
3n+2nlog3 n+15 | O(nlogn) | nlogn阶 |
4n^3+3n^2+22n+100 | O(n3) | 立方阶 |
2^n | O(2n) | 指数阶 |
- O(1)<O(logn)<O(n)<O(nlogn)<O(n2)<O(n3)<O(2n)<O(n!)<O(nn)
说明:
- 对数阶一般省略底数
- 因为所有的对数都可以转换为log2 n = log2 9 * log9 n
4.时间复杂度
- 时间复杂度:估算程序指令的执行次数
时间复杂度为O(n)
int A(int x, int n) {
int sum = 1; // 注意 任何数的0次方等于1
for (int i = 0; i < n; i++) {
sum = sum * x;
}
return sum;
}
时间复杂度为O(n)
int A(int x, int n) {
if (n == 0) {
return 1; // return 1 同样是因为0次方是等于1的
}
return A(x, n - 1) * x;
}
- 递归算法的时间复杂度本质上是要看: 递归的次数 * 每次递归中的操作次数。
时间复杂度为O((logn)
int sum = 1;
while(sum < n){
sum = sum * 2;
}
5.空间复杂度
- 空间复杂度:估算所需占用的存储空间
空间复杂度为O(1)
int j = 0;
for (int i = 0; i < n; i++) {
j++;
}
空间复杂度为O(n)
int[] a = new int[n];
for(int i = 0; i < n; i++){
a[i]=i;
}
边栏推荐
- LCD parameter interpretation and calculation
- Idea common shortcut keys
- SQL游标(cursor)详细说明及内部循环使用示例
- Byte flybook Human Resources Kit three sides
- Vulnhub[DC3]
- Research results of low code platform
- Gossip about the source code of redis 89
- C business serial number rule generation component
- vant3+ts 封装uploader上传图片组件
- js求斐波那契数列
猜你喜欢
Vulnhub[DC3]
Small program +app, a low-cost and active technology combination idea
ES7 does not use parent-child and nested relationships to implement one to many functions
1.5 what is an architect (serialization)
vant3+ts DropdownMenu 下拉菜单,数据多能滚动加载
General differences between SQL server versions released by Microsoft in different periods so far, for reference
极限编程--根源分析实践
vant3+ts 封装uploader上传图片组件
DRM driven MMAP detailed explanation: (I) preliminary knowledge
Extreme Programming -- Practice of root cause analysis
随机推荐
TypeScript常用类型(一)
C brief introduction
EasyCode模板
USB to serial port - maximum peak serial port baud rate vs maximum continuous communication baud rate
leetcode 647. 回文子串
[csp]202012-2 optimal threshold for period end forecast
Click the list page of vant3+ts+pinia tab to enter the details. The tab on the details page is highlighted in the original position, and the refresh highlight is in the first item by default
ESP32-C3 ESP-IDF 配置smartconfig 和 sntp 获取网络时间
Authorization in Golang ProjectUseing Casbin
USB to serial port - serial port driver type
Kali2022 installing Armitage
Original error interface
grpc-swift入门
JS中的数组(含leetcode例题)<持续更新~>
leetcode 647. Palindrome substring
SSM集成FreeMarker以及常用语法
String s = null ; String s = new String(); String s = "; what is the difference between string s?
Is it safe to open an account in flush
118. Yanghui triangle (dynamic planning)
Channel Original