当前位置:网站首页>[merge array] 88 merge two ordered arrays
[merge array] 88 merge two ordered arrays
2022-07-05 05:16:00 【lee2813】
One 、 subject
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 .
Example 1:
Input :nums1 = [1,2,3,0,0,0], m = 3, nums2 = [2,5,6], n = 3
Output :[1,2,2,3,5,6]
explain : Need merger [1,2,3] and [2,5,6] .
The combined result is [1,2,2,3,5,6] , In which, bold italics indicates nums1 The elements in .
Example 2:
Input :nums1 = [1], m = 1, nums2 = [], n = 0
Output :[1]
explain : Need merger [1] and [] .
The combined result is [1] .
Example 3:
Input :nums1 = [0], m = 0, nums2 = [1], n = 1
Output :[1]
explain : The array to be merged is [] and [1] .
The combined result is [1] .
Be careful , because m = 0 , therefore nums1 No elements in .nums1 The only remaining 0 Just to ensure that the merged results can be successfully stored in nums1 in .
Two 、 Answer key
Solution 1 : Violence solution
This problem can be solved without double pointers , Point to inserting another array completely after the first array , And then sort .
Solution 2 : Double pointer
Although this question points to two arrays , In each array, you find an element , And they are all orderly , So we can use double pointers .
- First , The initialization pointer points to the initial position of the two pointers , Because the length of the last interval is m + n So you can point the first pointer to the last bit of the first array , The second pointer points to the last bit of the second array . Compare in turn , Put the larger number forward by the last digit after expansion .
- secondly , Because the length of the two arrays may be different , There are two situations (1) The first array element is finished , The second array is not finished : At this time, you need to put all the values of the second array into (2) The first array element is not finished , The second array is finished : There is no need to deal with , Because this means that the elements in the first array are in the final position
3、 ... and 、 Code
class Solution {
public:
void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {
int pos = m-- + n-- - 1;
while(m>=0 & n>=0 & pos>=0){
nums1[pos--] = nums1[m] > nums2[n] ?nums1[m--]:nums2[n--];
}
while(n>=0){
nums1[pos--] = nums2[n--];
}
}
};
边栏推荐
- JVM call not used once in ten years
- 2022/7/1学习总结
- Magnifying glass effect
- SDEI初探-透过事务看本质
- 使用Room数据库报警告: Schema export directory is not provided to the annotation processor so we cannot expor
- Simple HelloWorld color change
- 2022/7/2 question summary
- Django reports an error when connecting to the database. What is the reason
- [leetcode] integer inversion [7]
- Cocos2dx screen adaptation
猜你喜欢
[paper notes] multi goal reinforcement learning: challenging robotics environments and request for research
[turn]: OSGi specification in simple terms
Grail layout and double wing layout
How to choose a panoramic camera that suits you?
嵌入式数据库开发编程(六)——C API
Do a small pressure test with JMeter tool
win10虚拟机集群优化方案
Unity parallax infinite scrolling background
2022/7/2 question summary
Leetcode word search (backtracking method)
随机推荐
Listview pull-down loading function
[转]MySQL操作实战(一):关键字 & 函数
[turn]: OSGi specification in simple terms
Common technologies of unity
64 horses, 8 tracks, how many times does it take to find the fastest 4 horses at least
2022/7/2做题总结
Research and investment forecast report of adamantane industry in China (2022 Edition)
UE fantasy engine, project structure
The next key of win generates the timestamp file of the current day
Learning notes of "hands on learning in depth"
Optimization scheme of win10 virtual machine cluster
cocos2dx_ Lua particle system
Transport connection management of TCP
[转]: OSGI规范 深入浅出
Research on the value of background repeat of background tiling
Personal required code
十年不用一次的JVM调用
[paper notes] multi goal reinforcement learning: challenging robotics environments and request for research
Grail layout and double wing layout
Sqlserver stored procedures pass array parameters