当前位置:网站首页>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--;
}
}边栏推荐
- Deep learning medical image model reproduction
- Problems encountered when the registry service Eureka switches to nocas
- PyTorch 使用 MaxPool 实现图像的膨胀和腐蚀
- openjudge:过滤多余的空格
- Digital twin solutions inject new momentum into the construction of chemical parks
- Openjudge: find all substring positions
- IO流的使用
- 使用深度学习训练图像时,图像太大进行切块训练预测
- JVM篇 笔记3:类加载与字节码技术
- About localdatetime in swagger
猜你喜欢

论文模型主图范例

冶金物理化学复习 -- 金属电沉积过程中的阴极极化,超电势以及阳极和阳极过程

冶金物理化学复习 --- 金属的电沉积,还原过程

Using Navicat or PLSQL to export CSV format, more than 15 digits will become 000 (e+19) later

Custom JSON return data

latex和word之间相互转换

冶金物理化学复习 ---- 气固反应动力学

冶金物理化学复习 --- 液 - 液相反应动力学

RESNET structure comparison

Invalid bound statement (not found): com.exam.mapper.UserMapper.findbyid
随机推荐
24小时内的时间段无交叉
Multi module packaging: package: XXX does not exist
Openjudge: perpetual calendar
(黑马)MYSQL初级-高级笔记(博主懒狗)
蒙特卡罗方法求解圆周率π并用turtle画点,以及完成进度条问题
集合框架的操作使用
Invalid bound statement (not found): com.exam.mapper.UserMapper.findbyid
The way of deep learning thermodynamic diagram visualization
VMware Workstation is incompatible with device/credential guard. Disable device/credential guard
mybaties foreach多选查询,index循环,取消and/or标签
Mybats foreach multi select query, index loop, and cancel the and/or tag
冶金物理化学复习 --- 气-液相反应动力学
pytorch 计算模型的GFlops和total params的方法
Export excel, generate multiple sheet pages, and name them
JVM篇 笔记4:内存模型
Example of main diagram of paper model
导出excel,生成多个sheet页,并命名
Video twins: the starting point of informatization upgrading of smart Parks
Database interview
Redis' bloom filter