当前位置:网站首页>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
边栏推荐
- Motion capture system for apple picking robot
- How does win11 set user permissions? Win11 method of setting user permissions
- 怎麼用MySQL語言進行行列裝置?
- 自动、智能、可视!深信服SSLO方案背后的八大设计
- StoneDB 为国产数据库添砖加瓦,基于 MySQL 的一体化实时 HTAP 数据库正式开源!
- Crypto Daily:孙宇晨在MC12上倡议用数字化技术解决全球问题
- Huawei issued hcsp-solution-5g security talent certification to help build 5g security talent ecosystem
- Korean AI team plagiarizes shock academia! One tutor with 51 students, or plagiarism recidivist
- Does 1.5.1 in Seata support mysql8?
- Thinkphp内核工单系统源码商业开源版 多用户+多客服+短信+邮件通知
猜你喜欢

周少剑,很少见

AVL balanced binary search tree

Sales management system of lightweight enterprises based on PHP

毕业后5年,我成为了年薪30w+的测试开发工程师

【IDM】IDM下载器安装

ATSs: automatically select samples to eliminate the difference between anchor based and anchor free object detection methods

【开源数据】基于虚拟现实场景的跨模态(磁共振、脑磁图、眼动)人类空间记忆研究开源数据集

【Hot100】19. Delete the penultimate node of the linked list

process.env.NODE_ENV

马来西亚《星报》:在WTO MC12 孙宇晨仍在坚持数字经济梦想
随机推荐
Automatique, intelligent, visuel! Forte conviction des huit conceptions derrière la solution sslo
2023届春招实习-个人面试过程和面经分享
德国iF多项大奖加冕,这副耳机有多强?音珀GTW 270 Hybrid深度评测
复杂度相关OJ题(LeetCode、C语言、复杂度、消失的数字、旋转数组)
Problèmes rencontrés dans le développement de la GI pour maintenir le rythme cardiaque en vie
制造业数字化转型究竟是什么
[200 opencv routines] 216 Draw polylines and polygons
[SQL statement] Why do you select two Shanghai and query different counts here? I want it to become a Shanghai, and count only displays a sum
【IDM】IDM下载器安装
Vscode find and replace the data of all files in a folder
Overview | slam of laser and vision fusion
Advanced cross platform application development (24): uni app realizes file download and saving
Telecommuting experience? Let's introduce ourselves ~ | community essay solicitation
Pocket network supports moonbeam and Moonriver RPC layers
实现数字永生还有多久?元宇宙全息真人分身#8i
【Hot100】17. 电话号码的字母组合
How to adjust the color of the computer screen and how to change the color of the computer screen
智慧党建: 穿越时空的信仰 | 7·1 献礼
从大湾区“1小时生活圈”看我国智慧交通建设
接口测试框架中的鉴权处理


