当前位置:网站首页>Some new ideas about time complexity
Some new ideas about time complexity
2022-07-29 02:47:00 【dfnsyyds】
1. Time complexity
- Generally, the time complexity only considers the worst case
- The time complexity of recursion is related to the depth of recursion . Such as calculating factorial :
// Compute factorial recursion Fac Time complexity of ?
long long Fac(size_t N)
{
if (0 == N)
return 1;
return Fac(N - 1) * N;
}
Recursion N Time , Each time a new function stack frame is opened , For example Fn, need Fn-1, Continue down to f1, Open up the following stack frame, and the upper stack frame is still in use , Because of the above Fn, Wait for the following return , So the complexity is N. So the complexity of space is N
3. The time complexity of Fibonacci sequence is :O(n).
Recall the bottom oblique triangle . Shared by painting trees N layer . Sum the number of calculations and N relevant 
- The best case for bubble sorting is O(N), The first round of bubbling Set the tag variable , If exchage Never changed , The statement is in order , There is no need to walk repeatedly , The code implementation is as follows :
// Calculation BubbleSort Time complexity of ?
void BubbleSort(int* a, int n)
{
assert(a);
for (size_t end = n; end > 0; --end)
{
int exchange = 0;
for (size_t i = 1; i < end; ++i)
{
if (a[i-1] > a[i])
{
Swap(&a[i-1], &a[i]);
exchange = 1;
}
}
if (exchange == 0)
break;
}
}
The number of force buckles disappearing :
https://leetcode.cn/problems/missing-number-lcci/
Array nums Contains from 0 To n All integers of , But one of them is missing . Please write code to find the missing integer . You have a way O(n) Is it finished in time ?
Ideas : It's actually a math problem
- Calculation 0~n And :( The first + tail )*n/2.
- Traverse and subtract all the numbers in the array , The residual value is missing .
The number of occurrences of numbers in an array
https://leetcode.cn/problems/shu-zu-zhong-shu-zi-chu-xian-de-ci-shu-lcof/
Ideas :
- The result of all digital XORs , In fact, it is the result of the XOR of two different numbers , But where two numbers are different , The binary form of their XOR result must have a bit 1, XOR is easy to do , loop ^, So let's find this first 1 Where? : Move right i position , Look and see 1 The result is 1.
- According to what is said above 1 The position of is divided into two groups of numbers , Of these two sets of numbers , Each contains a single occurrence number , The remaining figures appear twice .
for (int i = 1; i < 32; i++)
{
// Shift right and 1 Is a bit value
if ((ans >> i) & 1 == 1)
{
pos = i;
break;
}
}```
- Cycle through each number , If this bit matches, it will be shifted to 1&1==1, be
if ((* (nums + i) >> pos & 1)== 1)
{
s1^=*(nums+i);
}
- As long as there is a single occurrence of the number , use ret Another XOR will get . Because a number is XOR twice a number , Get yourself .
边栏推荐
- CUDA details GPU architecture
- 并发模式之生产者消费者模式
- Youxuan software appoints Huang Zhijun as the general manager of the company
- DHCP协议详细解析
- This blogger has a comprehensive classification of QT. If you are free, go to study and summarize it and record it.
- Tesla neural network model hydranet
- Brief answer of Engineering Economics
- QT qstringlist usage
- C语言:小乐乐与欧几里得
- XSS靶场(二)xss.haozi
猜你喜欢

自动分账系统哪家好?

XSS靶场(二)xss.haozi

新版海螺影视主题模板M3.1全解密版本多功能苹果CMSv10后台自适应主题开源全解密版

Shell script quick start-01

ECCV 2022 | airdet: a small sample target detection method without fine tuning

OSPF实验

PHP lucky draw system with background source code

【报错】node:internal/modules/cjs/loader:936 【解决方法】

Shell 脚本 快速入门 -01

Three implementation methods of Servlet
随机推荐
第七天笔记
ECCV 2022 | AirDet:无需微调的小样本目标检测方法
C语言:小乐乐与进制转换
CatchAdmin实战教程(四)Table组件相关功能实现
11.书写规则-伪目标
Redis master-slave mode, sentinel cluster, fragment cluster
Double write consistency of MySQL and redis
自组织是管理者和成员的双向奔赴
C language to achieve the three chess game
PHP lucky draw system with background source code
网络基础概论
owt-server源码剖析(三)--video模块分析之Mixer In
Where, having, group by, order by, is null, not in, subquery, delete, date function
A good-looking IAPP donation list source code
新版海螺影视主题模板M3.1全解密版本多功能苹果CMSv10后台自适应主题开源全解密版
多年前的回忆
idea配置web容器与war打包
Mqtt routine
Cuda-npp image and video processing
Source code of Jiugongge heart puzzle Applet / source code of main wechat applet with traffic