当前位置:网站首页>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
边栏推荐
- Advanced cross platform application development (24): uni app realizes file download and saving
- 【LeetCode】43. String multiplication
- [每日一氵]Latex 的通讯作者怎么搞
- Summer Challenge harmonyos canvas realize clock
- Nuxt.js数据预取
- u本位合约和币本位合约有区别,u本位合约会爆仓吗
- 自動、智能、可視!深信服SSLO方案背後的八大設計
- In the past six months, it has been invested by five "giants", and this intelligent driving "dark horse" is sought after by capital
- There is a difference between u-standard contract and currency standard contract. Will u-standard contract explode
- Use Tencent cloud to build a map bed service
猜你喜欢

Im instant messaging develops a message delivery scheme for 10000 people

周少剑,很少见

工厂高精度定位管理系统,数字化安全生产管理

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

PostgreSQL 存储结构浅析

AVL balanced binary search tree

Please, stop painting star! This has nothing to do with patriotism!

Win11如何設置用戶權限?Win11設置用戶權限的方法

近半年内连获5家“巨头”投资,这家智能驾驶“黑马”受资本追捧

2023 spring recruitment Internship - personal interview process and face-to-face experience sharing
随机推荐
【观察】数字化时代的咨询往何处走?软通咨询的思与行
投稿开奖丨轻量应用服务器征文活动(5月)奖励公布
Pico, do you want to save or bring consumer VR?
How to write good code - Defensive Programming Guide
Crypto Daily:孙宇晨在MC12上倡议用数字化技术解决全球问题
芯片供应转向过剩,中国芯片日产增加至10亿,国外芯片将更难受
Idea start command line is too long problem handling
Action after deleting laravel's model
In the past six months, it has been invested by five "giants", and this intelligent driving "dark horse" is sought after by capital
Share the daily work and welfare of DJI (Shenzhen headquarters) in Dajiang
[PHP graduation design] design and implementation of textbook management system based on php+mysql+apache (graduation thesis + program source code) -- textbook management system
[每日一氵]Latex 的通讯作者怎么搞
Use Tencent cloud to build a map bed service
[pyGame practice] do you think it's magical? Pac Man + cutting fruit combine to create a new game you haven't played! (source code attached)
2022 Moonriver全球黑客松优胜项目名单
从大湾区“1小时生活圈”看我国智慧交通建设
Can't global transactions be used when shardingjdbc is used in seate?
分享在大疆DJI(深圳总部)工作的日常和福利
2023届春招实习-个人面试过程和面经分享
Korean AI team plagiarizes shock academia! One tutor with 51 students, or plagiarism recidivist


