当前位置:网站首页>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
边栏推荐
- ssm框架原理
- 马来西亚《星报》:在WTO MC12 孙宇晨仍在坚持数字经济梦想
- Crypto Daily:孙宇晨在MC12上倡议用数字化技术解决全球问题
- [daily news]what happened to the corresponding author of latex
- 使用腾讯云搭建图床服务
- How to use MySQL language for row and column devices?
- 德国iF多项大奖加冕,这副耳机有多强?音珀GTW 270 Hybrid深度评测
- 自动、智能、可视!深信服SSLO方案背后的八大设计
- 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
- 2023 spring recruitment Internship - personal interview process and face-to-face experience sharing
猜你喜欢

Guide for high-end programmers to fish at work

Learn selenium to simulate mouse operation, and you can be lazy a little bit

How to write good code - Defensive Programming Guide

IM即时通讯开发万人群聊消息投递方案

嵌入式开发:5个修订控制最佳实践

StoneDB 为国产数据库添砖加瓦,基于 MySQL 的一体化实时 HTAP 数据库正式开源!

Pico, do you want to save or bring consumer VR?

复杂度相关OJ题(LeetCode、C语言、复杂度、消失的数字、旋转数组)

Sales management system of lightweight enterprises based on PHP

Please, stop painting star! This has nothing to do with patriotism!
随机推荐
IM即時通訊開發實現心跳保活遇到的問題
Overview | slam of laser and vision fusion
Pico, do you want to save or bring consumer VR?
C#/VB. Net merge PDF document
2022 Moonriver全球黑客松优胜项目名单
u本位合约和币本位合约有区别,u本位合约会爆仓吗
Win11如何設置用戶權限?Win11設置用戶權限的方法
The latest NLP game practice summary!
AVL balanced binary search tree
【Hot100】17. Letter combination of telephone number
Motion capture system for apple picking robot
For the sustainable development of software testing, we must learn to knock code?
ADS算力芯片的多模型架构研究
IM即时通讯开发万人群聊消息投递方案
使用腾讯云搭建图床服务
Pico, can we save consumer VR?
Uncover the "intelligence tax" of mousse: spend 4billion on marketing, and only 7 invention patents
DO280管理应用部署--pod调度控制
Principle of motion capture system
毕业后5年,我成为了年薪30w+的测试开发工程师


