当前位置:网站首页>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
边栏推荐
- Telecommuting experience? Let's introduce ourselves ~ | community essay solicitation
- 复杂度相关OJ题(LeetCode、C语言、复杂度、消失的数字、旋转数组)
- 搜索框和按钮缩放时会有缝隙的bug
- 运动捕捉系统原理
- Sales management system of lightweight enterprises based on PHP
- Comment utiliser le langage MySQL pour les appareils de ligne et de ligne?
- For the sustainable development of software testing, we must learn to knock code?
- How to adjust the color of the computer screen and how to change the color of the computer screen
- #夏日挑战赛# HarmonyOS canvas实现时钟
- 接口测试框架中的鉴权处理
猜你喜欢
Factory high-precision positioning management system, digital safety production management
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
[每日一氵]Latex 的通讯作者怎么搞
Share the daily work and welfare of DJI (Shenzhen headquarters) in Dajiang
电脑照片尺寸如何调整成自己想要的
Submission lottery - light application server essay solicitation activity (may) award announcement
Some abilities can't be learned from work. Look at this article, more than 90% of peers
Do280 management application deployment - pod scheduling control
【观察】数字化时代的咨询往何处走?软通咨询的思与行
自动、智能、可视!深信服SSLO方案背后的八大设计
随机推荐
Programming examples of stm32f1 and stm32subeide - production melody of PWM driven buzzer
process.env.NODE_ENV
Tanabata confession introduction: teach you to use your own profession to say love words, the success rate is 100%, I can only help you here ~ (programmer Series)
搜索框和按钮缩放时会有缝隙的bug
vscode 查找 替换 一个文件夹下所有文件的数据
Automatique, intelligent, visuel! Forte conviction des huit conceptions derrière la solution sslo
Where should older test / development programmers go? Will it be abandoned by the times?
从大湾区“1小时生活圈”看我国智慧交通建设
普通二本,去过阿里外包,到现在年薪40W+的高级测试工程师,我的两年转行心酸经历...
Action after deleting laravel's model
揭秘慕思“智商税”:狂砸40亿搞营销,发明专利仅7项
Go language learning notes - Gorm use - table addition, deletion, modification and query | web framework gin (VIII)
制造业数字化转型究竟是什么
How long will it take to achieve digital immortality? Metacosmic holographic human avatar 8i
Use Tencent cloud to build a map bed service
Embedded development: five revision control best practices
The latest NLP game practice summary!
Malaysia's Star: Sun Yuchen is still adhering to the dream of digital economy in WTO MC12
【LeetCode】43. String multiplication
分享在大疆DJI(深圳总部)工作的日常和福利