当前位置:网站首页>OJ questions related to complexity (leetcode, C language, complexity, vanishing numbers, rotating array)
OJ questions related to complexity (leetcode, C language, complexity, vanishing numbers, rotating array)
2022-07-01 16:16:00 【How to write the most elegant code】
Two about complexity LeetCode OJ topic
Code :C Language
List of articles
1. Missing numbers
Click to go to LeetCode see Missing numbers
1.1 Title Description
requirement : The time complexity should be within O(n) Inside
1.2 Solution and code implementation
1.2.1 Ideas 1
- malloc An additional number is n+1 Array of , And all initialized to -1.
- Traverse the data to be entered , What is this number , Fill in the corresponding position of the array .
- Traverse the array again , Which position is -1, Then the subscript of which position is the missing number .
ps:Drawio Your drawing is very good !!! If necessary, you can guide Microsoft Store Search apps Drawio, Download the genuine version directly .
- Time complexity :O(n), Meet the requirements of the topic
- Spatial complexity :O(n)
- The code part is also relatively simple , It's not going to happen here .
1.2.2 Ideas 2
- Use XOR , Here, I'll first give you some knowledge about XOR .
- What is XOR ?
In logic , Logical arithmetic is different from or (exclusive or) Is a logical disjunctive type of two operands , The symbol is XOR or EOR or ⊕( Commonly used in programming languages ^).
But different from the general logic or , The value of the exclusive or operator is true only if one of the two operands is true , And the value of the other is not true . Into a proposition , Namely :“ The two values are different .” or “ There is and only one is true .”- The characteristic of XOR :
- The result of two identical numbers exclusive or is 0.
- XOR is out of order . Bitwise XOR , Same as 0, Dissimilarity is 1.
- 0 And any number exclusive or result is the number itself
Let's get to the point , The solution is as follows :
- Create a variable x, And the initial value is 0
- x It is XOR with the input data
- x and 0~n The numbers between are exclusive or once , final x That is, the missing number
C The language code is implemented as follows :
int missingNumber(int* nums, int numsSize) {
int x = 0;
for (int i = 0; i < numsSize; ++i)
{
x ^= nums[i];
}
for (int j = 0; j < numsSize+1; ++j)
{
x ^= j;
}
return x;
}
- Time complexity :O(n)
- Spatial complexity :O(1)
2. Rotation array
Click to go to LeetCode see Rotation array
2.1 Title Description
Rotation array ( Rotated array ) Requirements are as follows :
- Think of three different solutions
- Time complexity O(n)
- Spatial complexity O(1)
2.2 Solution and code implementation
2.2.1 Ideas 1
- Rotate one at a time
- rotate K Time
- Time complexity :O(N^2)
- Spatial complexity :O(1)
- Unqualified ( can't make a good living OJ)
2.2.2 Ideas 2
Space for time
- Open up an extra array
- Put the back of the original array k Copy data to the front of the new array k A place
- Put the front of the original array n-k Copy data to the back of the new array n-k A place
- Then copy the new array back to the original array , End of rotation
- Time complexity :O(n)
- Spatial complexity :O(n)
- Unqualified ( But I can pass OJ)
2.2.3 Ideas 3
- Put it back in place
- front n-k An inverse
- after k An inverse
- Global inversion
- Time complexity :O(n)
- Spatial complexity :O(1)
- In line with the question , It is the best solution and the hardest to think of
C The language code is implemented as follows :
int* reverse(int* nums, int left, int right)
{
while (left < right)
{
int tmp = nums[left];
nums[left] = nums[right];
nums[right] = tmp;
left++;
right--;;
}
return nums;
}
void rotate(int* nums, int numsSize, int k) {
k %= numsSize;
reverse(nums, numsSize - k, numsSize - 1);
reverse(nums, 0, numsSize - k - 1);
reverse(nums, 0, numsSize - 1);
}
Learning record :
- This article is arranged in 2022.6.21
- If there are mistakes, please give me more advice
边栏推荐
- Pocket network supports moonbeam and Moonriver RPC layers
- SQLServer查询: a.id与b.id相同时,a.id对应的a.p在b.id对应的b.p里找不到的话,就显示出这个a.id和a.p
- laravel的模型删除后动作
- Embedded development: five revision control best practices
- 从 MLPerf 谈起:如何引领 AI 加速器的下一波浪潮
- 【观察】数字化时代的咨询往何处走?软通咨询的思与行
- Crypto Daily:孙宇晨在MC12上倡议用数字化技术解决全球问题
- [200 opencv routines] 216 Draw polylines and polygons
- Advanced cross platform application development (24): uni app realizes file download and saving
- 复杂度相关OJ题(LeetCode、C语言、复杂度、消失的数字、旋转数组)
猜你喜欢
Crypto Daily:孙宇晨在MC12上倡议用数字化技术解决全球问题
Overview | slam of laser and vision fusion
Summer Challenge harmonyos canvas realize clock
STM32F1与STM32CubeIDE编程实例-PWM驱动蜂鸣器生产旋律
There is a difference between u-standard contract and currency standard contract. Will u-standard contract explode
Im instant messaging develops a message delivery scheme for 10000 people
一次革命、两股力量、三大环节:《工业能效提升行动计划》背后的“减碳”路线图...
【Hot100】20. Valid parentheses
Comment utiliser le langage MySQL pour les appareils de ligne et de ligne?
Use Tencent cloud to build a map bed service
随机推荐
Go language learning notes - Gorm use - table addition, deletion, modification and query | web framework gin (VIII)
高端程序员上班摸鱼指南
The latest NLP game practice summary!
最新NLP赛事实践总结!
2022-07-01日报:谷歌新研究:Minerva,用语言模型解决定量推理问题
搜索框和按钮缩放时会有缝隙的bug
Five years after graduation, I became a test development engineer with an annual salary of 30w+
Comment win11 définit - il les permissions de l'utilisateur? Win11 comment définir les permissions de l'utilisateur
How to adjust the size of computer photos to what you want
Does 1.5.1 in Seata support mysql8?
Idea start command line is too long problem handling
Sqlserver query: when a.id is the same as b.id, and the A.P corresponding to a.id cannot be found in the B.P corresponding to b.id, the a.id and A.P will be displayed
制造业数字化转型究竟是什么
2022 Moonriver全球黑客松优胜项目名单
Action after deleting laravel's model
怎么用MySQL语言进行行列装置?
分享在大疆DJI(深圳总部)工作的日常和福利
Crypto Daily: Sun Yuchen proposed to solve global problems with digital technology on MC12
process.env.NODE_ENV
Pico,是要拯救还是带偏消费级VR?