当前位置:网站首页>【时间复杂度和空间复杂度】
【时间复杂度和空间复杂度】
2022-06-26 15:47:00 【@slow_walker】
时间复杂度和空间复杂度
复杂度分析
衡量不同算法的优劣,主要还是根据算法所占的空间和时间两个维度去考虑。但是,世界上不会存在完美的代码,既不消耗最多的时间,也不占用最多的空间,鱼和熊掌不可得兼,那么我们就需要从中去寻找一个平衡点,使得写出一份较为完美的代码。
时间复杂度
时间复杂度:分析算法的执行的效率
常见的时间复杂度
O(1) < O(nlogn)<O(n) < O(n2) < O(2^n) < O(n!)
1.O(1)
int fun(int n)
{
int i = n;
int j = 3*n;
return i+j;
}
2.O(nlogn)
int fun(int n)
{
int i = 1;
while(i<= n)
{
i = i*2;
}
return i;
}
3.O(n)
int fun(int n)
{
sum = 0;
for(int i = 0;i<n;i++)
{
sum += i;
}
return sum;
}
4.O(mlogn)
int fun(int m,int n)
{
sum = 0;
for(int i = 0;i<m;i++)
{
for(int j = 0;j<n;j++)
{
sum+= i*j;
j = j*2;
}
}
return sum;
}
5.O(n2)
int fun(int n)
{
sum = 0;
for(int i = 0;i<n;i++)
{
for(int j =0;j<n;j++)
{
ssum+= i*j;
}
}
return sum;
}
空间复杂度
算法所占内存空间的大小:看变量语句是不占空间的
常见的空间复杂度O(1) <O(n) < O(n2)
1.O(1)
int fun(int n)
{
sum = 0;
for(int i = 0;i<n;i++)
{
sum += i;
}
return sum;
}
2.O(n)
int fun(int n)
{
int arr[N];
int i = 0;
while(i<= N)
{
i = i*2;
}
return i;
}
3.O(MN)
int fun(int m,int n)
{
int arr[M][N];
for(int i = 0;i<m;i++)
{
for(int j = 0;j>=n;j++)
{
sum += arr[i][j];
}
}
return sum;
}
边栏推荐
- Failed to get convolution algorithm. This is probably because cuDNN failed to initialize
- Auto Sharding Policy will apply Data Sharding policy as it failed to apply file Sharding Policy
- 人人都当科学家之免Gas体验mint爱死机
- Interview pit summary I
- svg环绕地球动画js特效
- 8 自定义评估函数
- Tencent Peking University's sparse large model training acceleration program het was selected into the VLDB of the international summit
- 现在券商的优惠开户政策是什么?现在在线开户安全么?
- 还存在过有键盘的kindle?
- 6 custom layer
猜你喜欢

「干货」NFT 上中下游产业链全景分析

NFT 平台安全指南(1)

Svg capital letter a animation JS effect

李飞飞团队将ViT用在机器人身上,规划推理最高提速512倍,还cue了何恺明的MAE...

Beijing University and Tencent jointly build angel4.0, and the self-developed in-depth learning framework "River map" is integrated into the ecology

El dialog drag and drop, the boundary problem is completely corrected, and the bug of the online version is fixed

How to identify contractual issues

5000字解析:实战化场景下的容器安全攻防之道

js文本滚动分散动画js特效

Audio and video learning (I) -- PTZ control principle
随机推荐
Application of ansible automation
9 use of tensorboard
STEPN 新手入門及進階
【leetcode】48. Rotate image
面试踩坑总结一
SVG大写字母A动画js特效
(1) Keras handwritten numeral recognition and recognition of self written numbers
js文本滚动分散动画js特效
AUTO sharding policy will apply DATA sharding policy as it failed to apply FILE sharding policy
5000字解析:实战化场景下的容器安全攻防之道
JVM笔记
1 张量的简单使用
JVM notes
10 tf.data
El dialog drag and drop, the boundary problem is completely corrected, and the bug of the online version is fixed
CNN optimized trick
「幹貨」NFT 上中下遊產業鏈全景分析
Anaconda3安装tensorflow 2.0版本cpu和gpu安装,Win10系统
Beijing University and Tencent jointly build angel4.0, and the self-developed in-depth learning framework "River map" is integrated into the ecology
11 introduction to CNN