当前位置:网站首页>Merge two ordered arrays of order table OJ
Merge two ordered arrays of order table OJ
2022-07-28 05:42:00 【zhengyawen666】
One Title Description
Here are two buttons Non decreasing order Array of arranged integers nums1 and nums2, There are two other integers m and n , respectively nums1 and nums2 The number of elements in .
Would you please Merge nums2 To nums1 in , Make the merged array press Non decreasing order array .
Be careful : Final , The merged array should not be returned by the function , It's stored in an array nums1 in . In response to this situation ,nums1 The initial length of is m + n, The top m Elements represent the elements that should be merged , after n Elements are 0 , It should be ignored .nums2 The length of is n .
source : Power button (LeetCode)
link :https://leetcode.cn/problems/merge-sorted-array
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Two Ideas
Merge two ordered arrays , Then you need to define three pointers . A pointer points to the end of the first array , The second pointer points to the end of the second array , These two pointers are used to adjust the order of the sorted array , Another pointer points to the last position of the array , Used to identify the location where data is stored .
Because it stores data from the back to the front , So compare the value pointed to by the first and second pointers , Take the larger one and put it at the position marked by the third pointer . Then continue to look for the next one , Then the third pointer should point forward . The pointer in the array that has taken out a data also needs to point forward .
Until the data in one of the two arrays is stored .
At this point we need to consider : If the data in the second array is stored first , Then there is no need to continue the operation , Because the data is ultimately stored in the first array , The second array is stored first , Explain that the remaining arrays are ordered .
If the first array is stored first , Then we need to store the elements in the second array in the first array in turn , Because after the previous comparison and sorting , The smallest element in the first array will also be larger than the largest element in the second array at this time , So directly storing the elements in the second array into the first array can ensure the order of the elements in the array .
The illustration :

Code implementation :
void merge(int* nums1, int nums1Size, int m, int* nums2, int nums2Size, int n){
int n1=m-1;
int n2=n-1;
int pos=m+n-1;
while(n1>=0&&n2>=0)
{
if(nums1[n1]>nums2[n2])
{
nums1[pos]=nums1[n1];
n1--;
pos--;
}
else
{
nums1[pos]=nums2[n2];
n2--;
pos--;
}
}
while(n2>=0)
{
nums1[pos]=nums2[n2];
n2--;
pos--;
}
}边栏推荐
- How to compare long and integer and why to report errors
- RESNET structure comparison
- ByteBuffer. Position throws exception illegalargumentexception
- docker 部署 mysql5.7.35
- pytorch使用hook获得特征图
- 冶金物理化学复习 ---- 气固反应动力学
- Openjudge: count the number of numeric characters
- [idea plug-in artifact] teaches you how to set all attributes in an entity class with one click of idea
- LocalDateTime去掉T,JSONField失效
- lamda 获取当前循环数,AtomicInteger
猜你喜欢
随机推荐
科研论文写作方法:在方法部分添加分析和讨论说明自己的贡献和不同
Localdatetime removes T, and jsonfield is invalid
openjudge:找出全部子串位置
Review of metallurgical physical chemistry --- liquid liquid reaction kinetics
深度学习热力图可视化的方式
日期类及其基本功能的实现
When SQL queries the list, the data is inconsistent twice, and limit is automatically added
Advanced multithreading: Lock strategy
Example of main diagram of paper model
mybaties foreach多选查询,index循环,取消and/or标签
openjudge:石头剪刀布
2021CSDN博客之星评选,互投
Lamda gets the current number of cycles, atomicinteger
深度学习医学图像模型复现
Redis 之布隆过滤器
Use of IO streams
sql 查询list时两次的数据不一致,自动加上了limit
openjudge:校园食宿预订系统
openjudge:过滤多余的空格
Review of Metallurgical Physical Chemistry - gas liquid phase reaction kinetics









