当前位置:网站首页>88. Merge two ordered arrays
88. Merge two ordered arrays
2022-07-03 05:48:00 【yitahutu79】
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 .
Tips :
nums1.length == m + n
nums2.length == n
0 <= m, n <= 200
1 <= m + n <= 200
-109 <= nums1[i], nums2[j] <= 109
Method 1 : Double finger needling
class Solution {
public:
void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {
int arr[205] = {
0};
for (int i = 0, n1 = 0, n2 = 0; i < m + n; i++){
if (n1 == m) arr[i] = nums2[n2++];
else if(n2 == n) arr[i] = nums1[n1++];
else if (nums1[n1] < nums2[n2]) arr[i] = nums1[n1++];
else arr[i] = nums2[n2++];
}
for (int i = 0, j = 0; i < m + n; i++) {
nums1[i] = arr[j++];
}
}
};
Method 2 :sort Sort
class Solution {
public:
void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {
for (int i = 0; i < n; i++) {
nums1[m + i] = nums2[i];
}
sort(nums1.begin(), nums1.end());
}
};
边栏推荐
- 2022.DAY592
- CAD插件的安裝和自動加載dll、arx
- 期末复习DAY8
- Sorry, this user does not exist!
- 2022.7.2 simulation match
- [teacher Zhao Yuqiang] MySQL high availability architecture: MHA
- Final review (Day7)
- The server data is all gone! Thinking caused by a RAID5 crash
- Altaro set grandfather parent child (GFS) archiving
- Final review (Day2)
猜你喜欢

大二困局(复盘)

How to set up altaro offsite server for replication

配置xml文件的dtd

Redis使用Lua脚本简介
![[written examination question analysis] | | get [sizeof and strlen] [pointer and array] graphic explanation + code analysis](/img/c6/8847218fa43c87e3eb51c021961eb7.jpg)
[written examination question analysis] | | get [sizeof and strlen] [pointer and array] graphic explanation + code analysis

Redhat7系统root用户密码破解

Configure DTD of XML file
![[trivia of two-dimensional array application] | [simple version] [detailed steps + code]](/img/84/98c1220d0f7bc3a948125ead6ff3d9.jpg)
[trivia of two-dimensional array application] | [simple version] [detailed steps + code]

Altaro set grandfather parent child (GFS) archiving
![[teacher Zhao Yuqiang] MySQL flashback](/img/93/75998e28fd309880661ea723dc8de6.jpg)
[teacher Zhao Yuqiang] MySQL flashback
随机推荐
Jetson AGX Orin 平台移植ar0233-gw5200-max9295相机驱动
EMD distance - example of use
redis 无法远程连接问题。
Final review (Day6)
[set theory] relational closure (relational closure related theorem)
[minesweeping of two-dimensional array application] | [simple version] [detailed steps + code]
Why is the website slow to open?
Beaucoup de CTO ont été tués aujourd'hui parce qu'il n'a pas fait d'affaires
[branch and cycle] | | super long detailed explanation + code analysis + a trick game
期末复习(Day2)
[explain in depth the creation and destruction of function stack frames] | detailed analysis + graphic analysis
Download the corresponding version of chromedriver
Installation du plug - in CAD et chargement automatique DLL, Arx
2022.DAY592
Sorry, this user does not exist!
2022.6.30DAY591
Final review (Day5)
Redis encountered noauth authentication required
2022.7.2 simulation match
ansible防火墙firewalld设置