当前位置:网站首页>Leetcode daily practice - 88. Merge two ordered arrays
Leetcode daily practice - 88. Merge two ordered arrays
2022-07-26 19:41:00 【An airliner flying to the stars】
Preface
Wassup guys! I am a Edison
It's today LeetCode Upper leetcode 88. Merge two ordered arrays
Let’s get it!

List of articles
1. Topic analysis
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 .
Example 1:
Example 2:
Example 3:
2. Title diagram
The decreasing Explanation of the meaning of : Not completely increasing , The value after the array will not be smaller than the value before .
And when the nums1 and nums2 After the merger , The same is Non decreasing order Array of ;
And it's about putting nums2 Merge into nums1 Medium , because num1 Have been to nums2 Opened space .
Train of thought
The first idea to be introduced today is Merger , First nums1 and nums2 The array is as follows 
And then we use Subscripts act as pointers , The pointer i Point to nums1 The first element of , The pointer j Point to nums2 The first element of ;
Then open up a new array nums3, also nums3 The size and nums1 equally , And the pointer dst Point to nums3 The first element of ; As shown in the figure 
First i The element pointed to is less than j Pointing elements , Then put i Put the smaller element pointed to num3 in , As shown in the figure 
And then the pointer i and dst Move backward at the same time 
next i And j The elements pointed to are equal , Then casually put one of the elements into nums3 in , Suppose that j Put in num3 in , As shown in the figure 
Then put the pointer j and dst Move backward at the same time 
next i The element pointed to is less than j Pointing elements , Then put i Put the smaller element pointed to num3 in , As shown in the figure 
And then the pointer i and dst Move backward at the same time 
next i The element pointed to is still less than j Pointing elements , Then put i Put the smaller element pointed to num3 in , As shown in the figure 
And then the pointer i and dst Move backward at the same time 
here nums1 The elements in have gone , So directly put nums2 Get the remaining elements in nums3 In the middle ;
The first j Pointing elements 5 Take it , then j and dst Move backward at the same time 
Finally, put j Pointing elements 6 Take it 
The idea is : Compare in turn , Put the small ones into the merge array every time .
But the biggest problem with this method is : You need to open an extra array , Then it does not meet the requirements of the topic
Train of thought two
The method of train of thought one is : Traverse the array from left to right , Take a small value ;
Then I can choose From right to left Take the larger value , And then to get nums1 In the array , because nums1 The array has been opened up , There is room for them to merge ;
Or use it Subscripts act as pointers , The pointer i Point to nums1 The last It works Elements , The pointer j Point to nums2 The last It works Elements ; And then the pointer dst Point to nums1 Of The location of the last space ;
As shown in the figure 
First j The element pointed to is greater than i Pointing elements , Then put j Put the element pointed to dst Point to the position , As shown in the figure 
And then the pointer dst and j Move forward one bit at the same time ( Who points to the big element , The pointer is subtracted )
next j The element pointed to is still greater than i Pointing elements , Then put j Put the element pointed to dst Point to the position , As shown in the figure 
And then the pointer dst and j Move forward one bit at the same time 
here i The element pointed to is greater than j Pointing elements , Then put i Put the element pointed to dst Point to the position , As shown in the figure 
And then the pointer dst and i Move forward one bit at the same time 
here i The element that points to is equal to j Pointing elements , Then just put an element into dst Point to the position ;
Assume that j Put the element pointed to , And then the pointer dst and j Move forward one bit at the same time 
You can see the pointer at this time j Have the nums2 The elements in are traversed , Go further and form A cross-border visit 了 ;
So when j After traversing , And that's it ;
But there are also the following situations 
because nums1 Each value in the array is greater than nums2 Of , So the pointer i I'm sure I'll put it in advance nums1 End of traversal , Then we need to put num2 Every element in gets nums1 Before 3 Take a seat 
So the core is to see which array is traversed first !
This method does not open up additional space , So the time complexity is O ( M + N ) O(M + N) O(M+N)
m by nums 1 Number of valid elements in ;
n by nums 2 Number of valid elements in .
3. Code implementation
Interface code
void merge(int* nums1, int nums1Size, int m, int* nums2, int nums2Size, int n){
int i = m - 1;
int j = n - 1;
int dst = m + n - 1;
// nums2 Finish first
while (i >= 0 && j >= 0) {
if (nums1[i] > nums2[j]) {
nums1[dst] = nums1[i];
--dst;
--i;
}
else {
nums1[dst] = nums2[j];
--dst;
--j;
}
}
// nums1 Finish first
while (j >= 0) {
nums1[dst] = nums2[j];
--dst;
--j;
}
}
In fact, it can be simplified again
void merge(int* nums1, int nums1Size, int m, int* nums2, int nums2Size, int n){
int i = m - 1;
int j = n - 1;
int dst = m + n - 1;
// nums2 Finish first
while (i >= 0 && j >= 0) {
if (nums1[i] > nums2[j]) {
nums1[dst--] = nums1[i--];
}
else {
nums1[dst--] = nums2[j--];
}
}
// nums1 Finish first
while (j >= 0) {
nums1[dst--] = nums2[j--];
}
}
Submit results 
边栏推荐
- TB 117-2013 US Federal mandatory regulations
- 配置服务器环境
- 手机app测试用例怎么写?手机app测试点有哪些?
- Cannot find current proxy: Set ‘exposeProxy‘ property on Advised to ‘true‘ to make it available
- Openstack virtual machine network card is renamed cirename0
- canvas概述
- Pycharm加载conda创建pytorch虚拟环境报错,在conda命令行正常
- EN 1504-7 products for protection and repair of concrete structures corrosion prevention of reinforcement - CE certification
- Redis介绍
- Detailed explanation of MySQL master-slave replication configuration
猜你喜欢

Test interview question set UI automated test

How to solve the problem that win11 has been switched on after upgrading

什么是服务器集群?海外服务器集群的优势?

conda+pytorch环境教程

基于华为云 IOT 设计智能称重系统 (STM32)【二】结尾有资料

知识管理系统是什么?你需要知道这些

Ijcai2022 meeting! Brescia et al. Tutorial of evidential reasoning and learning, describing its latest progress, with 96 slides attached
![[yolov5] - detailed version of training your own dataset, nanny level learning, logging, hand-in-hand tutorial](/img/34/5ab529ff6d8d0fd3827c440299964d.png)
[yolov5] - detailed version of training your own dataset, nanny level learning, logging, hand-in-hand tutorial

彻底关闭win10自动更新

用低代码搭建千人食品制造企业高效管理系统案例分析
随机推荐
AttributeError: ‘Upsample‘ object has no attribute ‘recompute_scale_factor‘
基于华为云 IOT 设计智能称重系统 (STM32)【一】
Cuda11.2 corresponding pytorch installation
服务发现原理分析与源码解读
Detailed tutorial on installing redis on Linux
彻底关闭win10自动更新
What is federated graph machine learning? A summary of the latest "federal map machine learning: concepts, techniques, and Applications" at the University of Virginia
conda+pytorch环境教程
手机app测试用例怎么写?手机app测试点有哪些?
Will 3R balanced financial products have risks? Is it risky?
Linear algebra Chapter 3 vector
C # create and read dat file cases
Save gas chitoken usage
首席信息官引导业务变革的指南
Redis6
C # upper computer development - modify the window icon and exe file Icon
Description of MDM separation of powers and classification and grading authority
Pads draw 2.54mm row needle
Openstack virtual machine network card is renamed cirename0
软件测试——自动化测试框架有哪些?